Hoekieee
Hoekieee

Reputation: 391

to_datetime in pandas changes the date of my datetime data

I use the following code to extract the datetime of a .csv file:

house_data = 'test_1house_EV.csv'

house1 = pandas.read_csv(house_data)
time = pandas.to_datetime(house1["localminute"])

The datetime data to be extracted are the 1440 minutes of September 1, 2017. However, after using to_datetime the minutes between 00:00 and 05:00 are placed on September 2. e.g. the original data looks like this:

28      2017-09-01 00:28:00-05
29      2017-09-01 00:29:00-05
             ...          
1411    2017-09-01 23:31:00-05
1412    2017-09-01 23:32:00-05

but the datetime data looks like this:

28     2017-09-01 05:28:00
29     2017-09-01 05:29:00
       ...        
1410   2017-09-02 04:30:00
1411   2017-09-02 04:31:00

Does anyone know how to fix this?

Upvotes: 2

Views: 191

Answers (2)

James
James

Reputation: 36791

You can slice off the last three characters of the date string before converting.

pd.to_datetime(house1.localminute.str[:-3])

Upvotes: 0

ShreyasG
ShreyasG

Reputation: 806

Use this, as per @James' suggestion:

pd.to_datetime(house1["localminute"], format='%Y-%m-%d %H:%M:%S-%f')

Upvotes: 1

Related Questions