Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将时间增量转换为整数_Python_Pandas - Fatal编程技术网

Python 将时间增量转换为整数

Python 将时间增量转换为整数,python,pandas,Python,Pandas,假设我有一个包含时间增量数据的熊猫系列。 事实上,它是通过获取DateTimeIndex与自身的移位版本之间的差异生成的,因此给出了连续时间戳之间的增量 看起来像 timestamp 2015-02-01 00:00:04 00:00:04 2015-02-01 00:00:08 00:00:04 2015-02-01 00:00:12 00:00:04 .... Name: timestamp, dtype: timedelta64[ns] 这些值显然是numpy.timede

假设我有一个包含时间增量数据的熊猫系列。 事实上,它是通过获取DateTimeIndex与自身的移位版本之间的差异生成的,因此给出了连续时间戳之间的增量

看起来像

timestamp
2015-02-01 00:00:04   00:00:04
2015-02-01 00:00:08   00:00:04
2015-02-01 00:00:12   00:00:04
....
Name: timestamp, dtype: timedelta64[ns]
这些值显然是numpy.timedelta64,但我需要以秒为单位。 关于这一点也有类似的问题,但我还没有看到关于熊猫0.16.1的答案

我试过的是:

ts.apply(lambda x: x.seconds)
这给出了一个

AttributeError:'numpy.timedelta64'对象没有属性'seconds'

然后尝试

numpy.int64(ts)

但这给了我一个数组。现在我知道我可以将其转换回一个系列,但在一个调用或映射函数中没有其他方法可以做到这一点吗?

以下内容对我很有用:

In [24]:

t="""index,timestamp
2015-02-01 00:00:04,00:00:04
2015-02-01 00:00:08,00:00:04
2015-02-01 00:00:12,00:00:04"""
s = pd.read_csv(io.StringIO(t),parse_dates=[0,1], squeeze=True, index_col=[0])
In [26]:

s.dt.second
Out[26]:
index
2015-02-01 00:00:04    4
2015-02-01 00:00:08    4
2015-02-01 00:00:12    4
dtype: int64

datetime数据类型值有一个访问器,您可以在其中访问秒属性。

是否运行版本0.15.0或更高版本?如果是这样的话,
ts.dt.second
。的确如此。(运行0.16.1,我将在文档中查找)如果timedelta在索引中怎么办?@Sören您应该能够只运行
df.index.seconds
,因此不需要
.dt