简体   繁体   English

Python Pandas阅读时间

[英]Python Pandas reading time

When I am reading a time data from an xlsx file into pandas. 当我将时间数据从xlsx文件读取到熊猫中时。 It reads as a decimal value 读取为十进制值

Example :9:23:27 AM is read as .391284722 I can fix it by converting it into time using format cell and select time. 示例:9:23:27 AM读为.391284722我可以通过使用格式单元格将其转换为时间并选择时间来修复它。 But I would prefer to use pandas all the way through and not excel. 但是我宁愿一直使用大熊猫,而不是擅长使用大熊猫。

When I call the value and convert it into a date time object df.TIME=pd.to_datetime(df.TIME) 当我调用该值并将其转换为日期时间对象df.TIME = pd.to_datetime(df.TIME)

It changes to this date 1970-01-01 Desired time is 9:23:27 AM 1970年1月1日更改为该日期,所需时间为9:23:27 AM

Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thank you 谢谢

Demo: 演示:

read that column as string: 将该列读取为字符串:

df = pd.read_excel(filename, dtype={'col_name':str})

In [51]: df
Out[51]:
          time
0   9:23:27 AM
1  12:59:59 AM

In [52]: df['time2'] = pd.to_timedelta(df['time'])

In [53]: df
Out[53]:
          time    time2
0   9:23:27 AM 09:23:27
1  12:59:59 AM 12:59:59

In [54]: df.dtypes
Out[54]:
time              object
time2    timedelta64[ns]
dtype: object

UPDATE: in order to convert a float number (# of seconds) read from Excel try the following: 更新:为了转换从Excel读取的浮点数(秒),请尝试以下操作:

Source DF: 来源DF:

In [85]: df
Out[85]:
       time
0  0.391285
1  0.391285
2  0.391285

Solution: 解:

In [94]: df['time2'] = pd.to_timedelta((df['time'] * 86400).round(), unit='s')

In [95]: df
Out[95]:
       time    time2
0  0.391285 09:23:27
1  0.391285 09:23:27
2  0.391285 09:23:27

In [96]: df.dtypes
Out[96]:
time             float64
time2    timedelta64[ns]
dtype: object

The question could use some clarifying for an end-purpose for the time-column. 这个问题可以为时间列的最终目的作一些澄清。 For general purposes though, try using the format keyword in to_datetime . 不过出于一般目的,请尝试在to_datetime使用format关键字。

df.TIME=pd.to_datetime(df.TIME, format='%I:%M%S %p')

See this website for formatting: http://strftime.org/ 请参阅此网站以获取格式: http : //strftime.org/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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