0

Can you please help me with the following issue? When I import a csv file I have a dataframe like smth like this:

df = pd.DataFrame(['29/12/17',
'30/12/17', '31/12/17', '01/01/18', '02/01/18'], columns=['Date'])

What I want is to convert `Date' column of df into Date Time object. So I use the code below:

 df['date_f'] = pd.to_datetime(df['Date'])

What I get is smth like this:

df1 = pd.DataFrame({'Date': ['29/12/17', '30/12/17', '31/12/17', '01/01/18', '02/01/18'], 
                'date_f':['2017-12-29T00:00:00.000Z', '2017-12-30T00:00:00.000Z', '2017-12-31T00:00:00.000Z', '2018-01-01T00:00:00.000Z', '2018-02-01T00:00:00.000Z']})

The question is, why am I getting date_f in the following format ('2017-12-29T00:00:00.000Z') and not just ('2017-12-29') and how can I get the later format ('2017-12-29')?

P.S. I you use the code above it will the date_f in the format that I need. However, if the data is imported from csv, it provides the date_f format as specified above

Alberto Alvarez
  • 805
  • 3
  • 11
  • 20

1 Answers1

2

use dt.date

df['date_f'] = pd.to_datetime(df['Date']).dt.date

or

df['date_f'] = pd.to_datetime(df['Date'], utc=False)

both cases will get same outputs

       Date     date_f
0  29/12/17 2017-12-29
1  30/12/17 2017-12-30
2  31/12/17 2017-12-31
3  01/01/18 2018-01-01
4  02/01/18 2018-02-01