简体   繁体   English

如何在 Python 中为时间序列数据添加秒数?

[英]How can I add seconds to time series data in Python?

So I have the following timestamps that belong to a TS in a pandas dataframe:因此,我在 pandas dataframe 中有以下属于 TS 的时间戳:

 Timestamp('2010-11-20 00:00:00'),
 Timestamp('2010-11-20 00:00:00'),
 Timestamp('2010-11-20 00:00:00'),
 Timestamp('2010-11-20 00:00:00'),
 Timestamp('2010-11-20 00:00:00'),
 Timestamp('2010-11-20 00:00:00'),
 Timestamp('2010-11-20 00:00:00'),
 Timestamp('2010-11-20 00:00:00'),
 Timestamp('2010-11-20 00:00:00'),
 Timestamp('2010-11-20 00:00:00'),
 Timestamp('2010-11-20 00:00:00'),
 Timestamp('2010-11-20 00:00:00'),

The original csv file has readings 60 readings for each minute passed, but the timestamp only has hh:mm (eg 13:23) and when I convert/parse the dates it just adds 00 to all the seconds entries.原始的 csv 文件每分钟读取 60 个读数,但时间戳只有hh:mm (例如 13:23),当我转换/解析日期时,它只是将 00 添加到所有秒条目。 Is there any pandas functionality to add seconds?是否有任何 pandas 功能可以增加秒数? The motivation behind this is so that it will graph nicely in matplotlib.这背后的动机是它可以在 matplotlib 中很好地绘制图形。 Currently I have 60 overlapping points for each minute, however I want the timestamps to be increasing eg 00:00:01, 00:00:02, 00:00:03, etc.目前我每分钟有 60 个重叠点,但是我希望时间戳增加,例如 00:00:01、00:00:02、00:00:03 等。

manually add seconds in there assuming they are sorted and increment is always 1 second:假设它们已排序并且增量始终为 1 秒,则手动添加秒数:

df = pd.Series([pd.Timestamp(2020,11,20,0,0)]*10)
df += pd.Series(pd.Timedelta(seconds=i) for i in range(10))
0   2020-11-20 00:00:00
1   2020-11-20 00:00:01
2   2020-11-20 00:00:02
3   2020-11-20 00:00:03
4   2020-11-20 00:00:04
5   2020-11-20 00:00:05
6   2020-11-20 00:00:06
7   2020-11-20 00:00:07
8   2020-11-20 00:00:08
9   2020-11-20 00:00:09
dtype: datetime64[ns]

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

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