2

Currently my dates are formatted as a string. I was able to get the string converted to date time using the following:

df['submitted_on'] = df['submitted_on'].apply(lambda x: dt.datetime.strptime(x, '%Y-%m-%d %H:%M:%S.%f'))

I would like to remove the time stamp, but I am having an awfully difficult time doing so. My preferred format is %Y%m%d. So I stumbled upon THIS page and added .date(). Resulting in below:

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

I am getting this value error and I am again lost at what to do to drop the time stamp. Any help is greatly appreciated.

ValueError: time data '2015-02-26 16:45:36.0' does not match format '%Y%m%d'

Community
  • 1
  • 1
anshanno
  • 344
  • 4
  • 21

3 Answers3

2

You can use normalize (docs).

dti = pd.DatetimeIndex(start='today', periods=4, freq='D')
dti

outputs

DatetimeIndex(['2016-08-04 14:30:34.447589', '2016-08-05 14:30:34.447589',
               '2016-08-06 14:30:34.447589', '2016-08-07 14:30:34.447589'],
              dtype='datetime64[ns]', freq='D')

And

dti.normalize()

outputs

DatetimeIndex(['2016-08-04', '2016-08-05', '2016-08-06', '2016-08-07'], dtype='datetime64[ns]', freq='D')

If it's a Series of Timestamps you can convert them with map.

Edit: @piRSquared's way is better in this case.

pd.to_datetime(dti).map(lambda dt: dt.date())

outputs

array([datetime.date(2016, 8, 4), datetime.date(2016, 8, 5),
       datetime.date(2016, 8, 6), datetime.date(2016, 8, 7)], dtype=object)
Alex
  • 18,484
  • 8
  • 60
  • 80
  • Thanks Alex. Is it practical to apply dti.normalize() to my entire dataframe or should I only normalize datetime columns? – anshanno Aug 05 '16 at 17:31
2

You could convert the Timestamp object to datetime.datetime object and extract the datetime.date part as shown:

In [7]: import pandas as pd

In [8]: print(pd.Timestamp('2015-02-26 16:45:36.0').to_datetime().date())
2015-02-26
<class 'datetime.date'>

Your desired format:

In [11]: print(pd.Timestamp('2015-02-26 16:45:36.0').to_datetime().date().strftime("%Y%m%d"))
20150226
<class 'str'>
Nickil Maveli
  • 29,155
  • 8
  • 82
  • 85
1
s = pd.Series(['2010-01-01 10:00', '2010-06-01 11:00'])

pd.to_datetime(pd.to_datetime(s).dt.date)
piRSquared
  • 285,575
  • 57
  • 475
  • 624