Reputation: 21345
I have a dataframe with some text dates in them. I would like to return the dates: year, month and day in separate columns. But in order to do that I need to first convert the text(from excel) to a date.
The code I have now is
def convertdate(dstring):
dt = time.strptime(dstring, date_format='%Y-%m-%d')
return dt
However it returns an: TypeError: strptime() takes no keyword arguments
Then instead of three separate functions for creating three separate columns, one for year, one for month, one for day.
Once the string is a date time object (dt), I believe the code is: return dt.year, return dt.month, return dt.day
.
I would like one function which adds three columns to my dataframe, is this possible?
Upvotes: 3
Views: 17660
Reputation: 711
Pandas now supports pandas.to_datetime
for converting str to date.
eg to create a new column of dates from a string col:
import pandas as pd
df['My Date'] = pd.to_datetime(df['Date Str'], format='%Y-%m-%d')
To split into numbers:
df['My Year'] = df['My Date'].apply(lambda x: x.year)
df['My Month'] = df['My Date'].apply(lambda x: x.month)
df['My Day'] = df['My Date'].apply(lambda x: x.day)
To split into string columns, use the .dt.strftime
, eg for a month name like "Jan"
df['Month'] = df['My Date'].dt.strftime('%b')
More info: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html https://pandas.pydata.org/docs/reference/api/pandas.Series.dt.html
Upvotes: 0
Reputation: 369064
Pass the format string as a positional argument.
>>> import time
>>> def convertdate(dstring):
... return time.strptime(dstring, '%Y-%m-%d')
...
>>> convertdate('2013-03-02')
time.struct_time(tm_year=2013, tm_mon=3, tm_mday=2, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=61, tm_isdst=-1)
BTW, time.strptime
returns time.struct_time
. Its attributes are prefixed with tm_
.
More preferably you can use datetime.datetime.strptime
:
>>> import datetime
>>> import pandas as pd
>>>
>>> def convertdate(dstring):
... return datetime.datetime.strptime(dstring, '%Y-%m-%d')
...
>>> dt = convertdate('2013-03-02')
>>> dt
datetime.datetime(2013, 3, 2, 0, 0)
>>> pd.DataFrame([{'year': dt.year, 'month': dt.month, 'day': dt.day}])
day month year
0 2 3 2013
Upvotes: 3