Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.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 使用中的DateTimeIndex打印时遇到空数据帧错误_Python_Pandas_Matplotlib - Fatal编程技术网

Python 使用中的DateTimeIndex打印时遇到空数据帧错误

Python 使用中的DateTimeIndex打印时遇到空数据帧错误,python,pandas,matplotlib,Python,Pandas,Matplotlib,我试图用Pandas plotting integration绘制时间序列,收到错误TypeError:Empty'DataFrame':没有要绘制的数值数据。直接使用matplotlib是可行的,但我认为我滥用了该库,希望确保我在使用Pandas时没有走错方向 以下是正在发生的事情: 我有一个包含两列的数据文件。第一列是时间戳,第二列是经过的时间。两者都以纳秒为单位。我正在使用以下方法读取数据: data = pd.read_table('trace.log', sep=" ", header

我试图用Pandas plotting integration绘制时间序列,收到错误
TypeError:Empty'DataFrame':没有要绘制的数值数据
。直接使用matplotlib是可行的,但我认为我滥用了该库,希望确保我在使用Pandas时没有走错方向

以下是正在发生的事情:

我有一个包含两列的数据文件。第一列是时间戳,第二列是经过的时间。两者都以纳秒为单位。我正在使用以下方法读取数据:

data = pd.read_table('trace.log', sep=" ", header=None,
    names=("start", "latency"))
print(data.head())
print(data.dtypes)
其中包含以下数据:

            start    latency
0  27668827345634  754210039
1  27668827918895  753710503
2  27668827809194  754495193
3  27668827974232  754464123
4  27669581667404   60338395
start      int64
latency    int64
dtype: object
然后我将
start
转换为
datetime64[ns]
并将其作为索引,然后将
latency
转换为
timedelta64[ns]

data.start = pd.to_datetime(data.start, unit="ns")
data.latency = pd.to_timedelta(data.latency, unit="ns")
data.set_index('start', inplace=True)

print(data.head())
print(data.dtypes)
print(data.index)
现在我有一个带有DateTimeIndex的时间序列,我的延迟用时间增量表示:

                                      latency
start                                        
1970-01-01 07:41:08.827345634 00:00:00.754210
1970-01-01 07:41:08.827495897 00:00:01.395999
1970-01-01 07:41:08.827574509 00:00:01.395592
1970-01-01 07:41:08.827605687 00:00:01.381083
1970-01-01 07:41:08.827634020 00:00:01.381130
latency    timedelta64[ns]
dtype: object
DatetimeIndex(['1970-01-01 07:41:08.827345634',
               '1970-01-01 07:41:08.827495897',
               ...
               '1970-01-01 08:11:07.739615123',
               '1970-01-01 08:11:07.756520620'],
              dtype='datetime64[ns]', name='start', length=437915, freq=None)
我看到的问题是,当我试图绘制这幅图时。根据我看到的示例,我应该能够运行:

data.latency.plot()
生成延迟与开始时间的曲线图,但我得到以下错误:

/opt/conda/lib/python3.5/site-packages/pandas/tools/plotting.py in _compute_plot_data(self)
   1092         if is_empty:
   1093             raise TypeError('Empty {0!r}: no numeric data to '
-> 1094                             'plot'.format(numeric_data.__class__.__name__))
   1095 
   1096         self.data = numeric_data

TypeError: Empty 'DataFrame': no numeric data to plot

请注意,如果我使用
plt.plot(data.index,data.latency)
绘制数据,那么我就得到了预期的结果。我想我一定是错过了一个关键的理解,否则我会看到一个bug。能够使用Pandas plotting integration会很好。

您可以使用
set\u major\u formatter()
自定义时间刻度:

import io
import datetime
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd

data = """\
            start    latency
0  27668827345634  754210039
1  27668827918895  753710503
2  27668827809194  754495193
3  27668827974232  754464123
4  27669581667404   60338395
"""
data = pd.read_csv(io.StringIO(data), sep='\s+', index_col=0)

data.start = pd.to_datetime(data.start, unit="ns")
# convert nanoseconds to seconds
data.latency /= 10**9

# define custom Ticker formatter function
def timeTicks(x, pos):
    return str(datetime.timedelta(seconds=x))

formatter = matplotlib.ticker.FuncFormatter(timeTicks)

ax = data.plot(x='start', y='latency')

# format yticks
ax.yaxis.set_major_formatter(formatter)

plt.show()

我不知道你指的是什么把戏。在plot命令中显式设置“x”数据是可行的,但我完全希望在不设置“x”的情况下打印变量会使用“x”数据的索引。@NoahWatkins,诀窍是使用
set\u major\u formatter()
。我明确地设置了
x
以保存(去除)
数据。设置索引('start')
,因为我认为您这样做只是为了绘图-如果是这样,您可以简单地指定
x
轴-它应该更快。