python用pandas读取数据时出现错误_python pandas错误地读取excel日期

1586010002-jmsa.png

I have an excel file with dates formatted as such:

22.10.07 16:00

22.10.07 17:00

22.10.07 18:00

22.10.07 19:00

After using the parse method of pandas to read the data, the dates are read almost correctly:

In [55]: nts.data['Tid'][10000:10005]

Out[55]:

10000 2007-10-22 15:59:59.997905

10001 2007-10-22 16:59:59.997904

10002 2007-10-22 17:59:59.997904

10003 2007-10-22 18:59:59.997904

What do I need to do to either a) get it to work correctly, or b) is there a trick to fix this easily? (e.g. some kind of 'round' function for datetime)

解决方案

I encountered the same issue and got around it by not parsing the dates using Pandas, but rather applying my own function (shown below) to the relevant column(s) of the dataframe:

def ExcelDateToDateTime(xlDate):

epoch = dt.datetime(1899, 12, 30)

delta = dt.timedelta(hours = round(xlDate*24))

return epoch + delta

df = pd.DataFrame.from_csv('path')

df['Date'] = df['Date'].apply(ExcelDateToDateTime)

Note: This will ignore any time granularity below the hour level, but that's all I need, and it looks from your example that this could be the case for you too.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值