0

I'm trying to convert a string to a date and I understand how to use the to_datetime that comes with pandas but I'd like to be able to do this without inserting a time? I'm sure this is very simple but I'm a little new to this.

Colin O'Brien
  • 2,175
  • 5
  • 20
  • 26

1 Answers1

1

You don't need the time component, if you use the datetime.strptime or to_datetime the conversion is the same:

In [10]:

df = pd.DataFrame({'date':['2012/04/06']})
df
Out[10]:
         date
0  2012/04/06
In [11]:

import datetime as dt
df['date'].apply(lambda x: dt.datetime.strptime(x, '%Y/%m/%d'))

Out[11]:
0   2012-04-06
Name: date, dtype: datetime64[ns]
In [13]:

pd.to_datetime(df['date'])
Out[13]:
0   2012-04-06
Name: date, dtype: datetime64[ns]
EdChum
  • 376,765
  • 198
  • 813
  • 562