0

I have a dataframe with column date which looks like this:

Feb 24, 2020 @ 12:47:31.616

I would like it to become this:

2020-02-24

I can achieve this using slicing since I am dealing only with one week's data hence all months will be Feb.

Is there a neat pandas way to change the datestamp to date format I desire?

Thank you for your suggestions.

Jonas Palačionis
  • 4,591
  • 4
  • 22
  • 55
  • How are you reading the data? You can specify a datetime format at read time. – Dan Feb 24 '20 at 13:46
  • Does this answer your question? [How to convert string to datetime format in pandas python?](https://stackoverflow.com/questions/32204631/how-to-convert-string-to-datetime-format-in-pandas-python) – Nabin Feb 24 '20 at 13:50
  • @Nabin - Only partial dupe, so no dupe – jezrael Feb 24 '20 at 13:51

2 Answers2

0

Use to_datetime with format %b %d, %Y @ %H:%M:%S.%f and then if necessary convert to dates by Series.dt.date or to datetimes by Series.dt.floor:

#dates
df = pd.DataFrame({'dates':['Feb 24, 2020 @ 12:47:31.616','Feb 24, 2020 @ 12:47:31.616']})
df['dates'] = pd.to_datetime(df['dates'], format='%b %d, %Y @ %H:%M:%S.%f').dt.date
#datetimes
df['dates'] = pd.to_datetime(df['dates'], format='%b %d, %Y @ %H:%M:%S.%f').dt.floor('d')
print (df)
        dates
0  2020-02-24
1  2020-02-24
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

Using pd.to_datetime with Series.str.split:

df = pd.DataFrame({'date':['Feb 24, 2020 @ 12:47:31.616']})

                          date
0  Feb 24, 2020 @ 12:47:31.616
df['date'] = pd.to_datetime(df['date'].str.split('\s@\s').str[0], format='%b %d, %Y')

        date
0 2020-02-24
Erfan
  • 40,971
  • 8
  • 66
  • 78