简体   繁体   中英

Read dates from excel to Pandas Dataframe

I have .xlsx excel file, which one of its column shows the dates (the first 6 rows are shown as below):

Recd_Date/Due_Date
01/31/2000
02/29/2000
02/01/2000
03/01/2000
02/02/2000
03/02/2000

After I used Pandas' read_excel() to convert the excel to pandas Dataframe, the 'Recd_Date/Due_Date' is shown as:

Recd_Date/Due_Date
2000-01-31 00:00:00
2000-02-29 00:00:00
2000-01-02 00:00:00
2000-01-03 00:00:00
2000-02-02 00:00:00
2000-02-03 00:00:00

type(df.ix[i]['Recd_Date/Due_Date']) = pandas.tslib.Timestamp

As you can see read_excel() got some of the dates wrong, eg 02/01/2000 becomes 2000-01-02 00:00:00 (it should be 2000-02-01 00:00:00); 03/02/2000 becomes 2000-02-03 00:00:00 ( should be 2000-03-02 00:00:00)...How can i make it understand the dates in the original excel file as month/day/year and so it can convert the dates correctly??

Many thanks!!

Why not create another column with the date inverted ? You can transform the code below into a function and use it.

data = '01/31/2000'
data2 = data.split("/")
data22 = []
for i in range(-1, -4, -1):
    data22.append(data2[i]) 

data3 = "/".join(data22)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM