2

I have 2 date type in one of python dataframe column, i want to all the data values into same format.

df = pd.DataFrame({'Date':['2016-06-01', '2016-06-01 10:00']})
print (df)
           Date
0        2016-06-01
1  2016-06-01 10:00

I want both the date in same format as below.

          Date
0  2016-06-01 00:00
1  2016-06-01 10:00

how to handle it using python.

Thanks

2 Answers2

2

I think use to_datetime:

df = pd.DataFrame({'Date':['2016-06-01', '2016-06-01 10:00']})

df['Date'] = pd.to_datetime(df.Date)
print (df)
                 Date
0 2016-06-01 00:00:00
1 2016-06-01 10:00:00
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • I hope my answer will be accepted, because I was first, but it is up to you. Thanks for upvote.Nice day! – jezrael Jun 10 '16 at 05:02
1

You could use pd.to_datetime for that:

In [3]: pd.to_datetime(df.Date)
Out[3]:
0   2016-06-01 00:00:00
1   2016-06-01 10:00:00
Name: Date, dtype: datetime64[ns]

And then if you want to show only hours and minutes you could use strftime from dt accessor:

In [10]: pd.to_datetime(df.Date).dt.strftime('%Y-%m-%d %H:%M')
Out[10]:
0    2016-06-01 00:00
1    2016-06-01 10:00
Name: Date, dtype: object
Anton Protopopov
  • 30,354
  • 12
  • 88
  • 93