Getting the current age using datetime library - python-3.x

I have a column of a user's DOB data in datetime64[ns] format, and would like to calculate their current age. Every time I parse this date and try to subtract the same with the present date, it throws me an error of str and datetime data format invalidity.
from datetime import datetime
main_file['AD_DOB'] = pd.to_datetime(main_file['AD_DOB']).dt.date ##10/2/1943
now = datetime.now().strftime("%H:%M:%S")
main_file['Age'] = ((now - main_file['AD_DOB'])/365).dt.days
Error: TypeError: unsupported operand type(s) for -: 'str' and 'datetime.date'

Try this
from datetime import datetime
main_file['AD_DOB'] = pd.to_datetime(main_file['AD_DOB']).dt.date ##10/2/1943
now = datetime.datetime.now()
main_file['Age'] = ((now - main_file['AD_DOB'])/365).dt.days

It's because that datetime.now().strftime("%H:%M:%S") returns a string object
Below should work:
now = datetime.now()
main_file['Age'] = (now - main_file['AD_DOB']).days/365.0

For subtract in pandas is necessary convert values to datetimes, if want remove times use Series.dt.floor for AD_DOB column and also for now is used Timestamp.floor:
main_file = pd.DataFrame({'AD_DOB':['10/2/1943','10/8/1946','10/12/1983']})
main_file['AD_DOB'] = pd.to_datetime(main_file['AD_DOB']).dt.floor('d')
now = pd.to_datetime('now').floor('d')
main_file['Age'] = ((now - main_file['AD_DOB'])/365).dt.days
print (main_file)
AD_DOB Age
0 1943-10-02 76
1 1946-10-08 73
2 1983-10-12 36

Related

I need to fetch the date from one field "from date" and increement 19 days and pass it into "to-date" in Robot framework.Is thr any other way to do it

How to fetch date and increment the date and pass into the other field in robot framework. I have tried the below code and didn't work.
from datetime import datetime, time delta
x = '2017-05-15'
res = (datetime.strptime(x, '%Y-%m-%d') + time delta(days=1)).strftime('%Y-%m-%d')
print(res)
# 2017-05-16

How can I create a column that flags when another datetime column has changed date?

How can I create a column 'Marker that flags (0 or 1) when another datetime column 'DT' has changed date?
df = pd.DataFrame()
df['Obs']=float_array
df['DT'] = pd.to_datetime(datetime_array)
df['Marker'] = 0
print(type(df['DT'].dt))
<class 'pandas.core.indexes.accessors.DatetimeProperties'>
df['Marker'] = df.where(datetime.date(df.DT.dt) == datetime.date(df.DT.shift(1).dt),1)
TypeError: an integer is required (got type DatetimeProperties)
Use Series.dt.date for convert to dates and for convert True/False to 1/0 is used Series.view:
df['Marker'] = (df.DT.dt.date == df.DT.dt.date.shift()).view('i1')
Or numpy.where:
df['Marker'] = np.where(df.DT.dt.date == df.DT.dt.date.shift(), 0, 1)

Set days since first occurrence based on multiple columns

I have a pandas dataset with this structure:
Date datetime64[ns]
Events int64
Location object
Day float64
I've used the following code to get the date of the first occurrence for location "A":
start_date = df[df['Location'] == 'A'][df.Events != 0].iat[0,0]
I now want to update all of the records after the start_date with the number of days since the start_date, where Day = df.Date - start_date.
I tried this code:
df.loc[df.Location == country, 'Day'] = (df.Date - start_date).days
However, that code returns an error:
AttributeError: 'Series' object has no attribute 'days'
The problem seems to be that the code recognizes df.Date as an object instead of a datetime. Anyone have any ideas on what is causing this problem?
Try, you need to add the .dt accessor.
df.loc[df.Location == country, 'Day'] = (df.Date - start_date).dt.days

How to extract specific set of date from a date column of an CSV file using Pandas?

I want to access some specific values from the date column from my CSV file and save to another.
here 'f' is the data frame of my csv file, "now" is the current date, "f['Date']"is the column which has to be manupilated,
LOGIC:
if current day is monday then im subtracting 2 days from the current date and then storing it in "day" and accessing the dates in f['Date'] column which are equivalent to the "day" date.
else we re subtracting only 1 day and doing the same.
now = date.today()
curr_day= now.strftime("%A")
now = now.strftime("%Y-%m-%d")
f['Date'] = pd.to_datetime(f['Date'])
# f['Date'] = f['Date'].apply( lambda x : x.strftime("%Y-%m-%d"))
f['Date'] = f['Date'].astype(str)
if curr_day == 'Monday':
day = datetime.datetime.strptime(now,"%Y-%m-%d").date() - timedelta(days=2)
day= day.strftime("%Y-%m-%d")
f['Date']=f.loc[f['Date']==day]
else:
day = datetime.datetime.strptime(now,"%Y-%m-%d").date() - timedelta(days=1)
day= day.strftime("%Y-%m-%d")
f['Date']=f.loc[f['Date']==day]
I have a date lets say: DATE1
and I need to access only those values in the date column which are equivalent to the DATE1 and the save to the new CSV file.
Date
01-Apr-19
02-Apr-19
03-Apr-19
14-Apr-19
18-Apr-19
14-Apr-19
14-Apr-19
14-Apr-19
01-Apr-19
10-Apr-19
01-Apr-19
01-Apr-19
Assume DATE1=14-04-2019
Then the updated CSV will look like
Date
14-Apr-19
14-Apr-19
14-Apr-19
14-Apr-19
I am assuming the date format you have given and adding my response. I am attaching the code for your solution!
from datetime import datetime, timedelta
import pandas as pd
data['date'] = pd.to_datetime(data['Date'],format='%d-%b-%y')
data['date'] = data['date'].apply(lambda x: x.date())
now = datetime.now()
day = now.strftime("%A")
if day=='Monday':
days_to_subtract = 2
date_filter = datetime.today() - timedelta(days=days_to_subtract)
new_data = data.loc[data['date']==(date_filter).date()]
new_data.drop('date',1,inplace = True)
new_data.reset_index(drop=True,inplace = True)
else:
days_to_subtract = 1
date_filter = datetime.today() - timedelta(days=days_to_subtract)
new_data = data.loc[data['date']==(date_filter).date()]
new_data.drop('date',1,inplace = True)
new_data.reset_index(drop=True,inplace = True)

convert scientific notation to datetime

How can I convert date from seconds to date format.
I have a table containing information about lat, long and time.
table
f_table['dt'] = pd.to_datetime(f_table['dt'])
f_table["dt"]
it results like this:
output
but the output is wrong actually the date is 20160628 but it converted to 1970.
My desired output:
24-April-2014
The unit needs to be nanoseconds, so you need to multiply with 1e9
f_table['dt'] = pd.to_datetime(f_table['dt'] * 1e9)
This should work.
#Split your string to extract timestamp, I am assuming a single space between each float
op = "28.359062 69.693673 5.204486e+08"
ts = float(op.split()[2])
from datetime import datetime
#Timestamp to datetime object
dt = datetime.fromtimestamp(ts)
#Datetime object to string
dt_str = dt.strftime('%m-%B-%Y')
print(dt_str)
#06-June-1986

Resources