Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.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_Matplotlib_Dataframe - Fatal编程技术网

Python 将一系列时间增量分割为逐分钟图(熊猫)

Python 将一系列时间增量分割为逐分钟图(熊猫),python,pandas,matplotlib,dataframe,Python,Pandas,Matplotlib,Dataframe,我有一个数据帧,索引为Timedelta,从0到5分钟不等,还有一列浮点数 下面是一个示例子集: 32 0.740283 34 0.572126 36 0.524788 38 0.509685 40 0.490219 42 0.545977 44 0.444170 46 1.098387 48 2.209113 51 1.426835 53 1.536439 55 1.196625 56 1.923569 左边是以秒为单位的时间增量,右边是浮点数 问题是当使用熊猫绘图

我有一个数据帧,索引为Timedelta,从0到5分钟不等,还有一列浮点数

下面是一个示例子集:

32  0.740283
34  0.572126
36  0.524788
38  0.509685
40  0.490219
42  0.545977
44  0.444170
46  1.098387
48  2.209113
51  1.426835
53  1.536439
55  1.196625
56  1.923569
左边是以秒为单位的时间增量,右边是浮点数

问题是当使用熊猫绘图时,我得到一个带有标签的x轴,例如:
0天00:00:00、0天00:01:10、0天00:02:15

等等。是否有任何方法可以对数据进行重新采样(错误的单词?),以便在保持数据点在正确位置的同时,逐分钟获取轴

示例代码/数据:

df = pd.DataFrame({'td':[32,34,36,38,40,42,44,51,53,152,283],
                   'val': np.random.rand(11)})

df.index = df.td.map(lambda x: pd.Timedelta(seconds=x.astype(int)))
df.drop(['td'], axis=1, inplace=True)
df.val.plot()
这是你想要的吗

import pandas as pd
import numpy as np
td = np.array([32,34,36,38,40,42,44,51,53,152,283])*1e9  # if you don't multiply by 1e9, then pandas will assume you are referring to nanoseconds when you use the function pd.to_datetime()
df = pd.DataFrame({'td':td,
                   'val': np.random.rand(11)})

df.index = pd.to_datetime(df.td)
df.index = df.index.time  # select the time component of the index ... ignoring the date
df.drop('td', 1, inplace=True)

print df
              val
00:00:32  0.825991
00:00:34  0.578752
00:00:36  0.348558
00:00:38  0.221674
00:00:40  0.706031
00:00:42  0.912452
00:00:44  0.448185
00:00:51  0.368867
00:00:53  0.188401
00:02:32  0.855828
00:04:43  0.494732

df.plot()  # it gets the plot you want

Pandas仅为方便起见提供绘图功能。要完全控制,需要直接使用Matplotlib

作为一种解决方法,您可以只使用datetime而不是timedelta作为索引。只要你的时间跨度在几分钟之内,熊猫就不会绘制日期或月份

以您的示例为例,这是有效的:

df = pd.DataFrame({'td':[32,34,36,38,40,42,44,51,53,152,283],
                   'val': np.random.rand(11)})
df.index = [dt(2010, 1, 1) + timedelta(seconds=int(i)) for i in df.td]
df.drop(['td'], axis=1, inplace=True)
df.val.plot()