Python 绘制熊猫 timedelta

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23543909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:06:51  来源:igfitidea点击:

Plotting pandas timedelta

pythonmatplotlibpandas

提问by DataSwede

I have a pandas dataframe that has two datetime64 columns and one timedelta64 column that is the difference between the two columns. I'm trying to plot a histogram of the timedelta column to visualize the time differences between the two events.

我有一个 Pandas 数据框,它有两个 datetime64 列和一个 timedelta64 列,这是两列之间的差异。我正在尝试绘制 timedelta 列的直方图以可视化两个事件之间的时间差异。

However, just using df['time_delta']results in: TypeError: ufunc add cannot use operands with types dtype('<m8[ns]') and dtype('float64')

但是,仅df['time_delta']在以下情况下使用结果: TypeError: ufunc add cannot use operands with types dtype('<m8[ns]') and dtype('float64')

Trying to convert the timedelta column to : float--> df2 = df1['time_delta'].astype(float)results in: TypeError: cannot astype a timedelta from [timedelta64[ns]] to [float64]

尝试将 timedelta 列转换为 :float--> df2 = df1['time_delta'].astype(float)结果: TypeError: cannot astype a timedelta from [timedelta64[ns]] to [float64]

How would one create a histogram of pandas timedelta data?

如何创建熊猫 timedelta 数据的直方图?

采纳答案by Jeff

Here are ways to convert timedeltas, docs are here

这是转换时间增量的方法,文档在这里

In [2]: pd.to_timedelta(np.arange(5),unit='d')+pd.to_timedelta(1,unit='s')
Out[2]: 
0   0 days, 00:00:01
1   1 days, 00:00:01
2   2 days, 00:00:01
3   3 days, 00:00:01
4   4 days, 00:00:01
dtype: timedelta64[ns]

Convert to seconds (is an exact conversion)

转换为秒(是精确转换)

In [3]: (pd.to_timedelta(np.arange(5),unit='d')+pd.to_timedelta(1,unit='s')).astype('timedelta64[s]')
Out[3]: 
0         1
1     86401
2    172801
3    259201
4    345601
dtype: float64

Convert using astype will round to that unit

使用 astype 转换将舍入到该单位

In [4]: (pd.to_timedelta(np.arange(5),unit='d')+pd.to_timedelta(1,unit='s')).astype('timedelta64[D]')
Out[4]: 
0    0
1    1
2    2
3    3
4    4
dtype: float64

Division will give an exact repr

司将给出准确的代表

In [5]: (pd.to_timedelta(np.arange(5),unit='d')+pd.to_timedelta(1,unit='s')) / np.timedelta64(1,'D')
Out[5]: 
0    0.000012
1    1.000012
2    2.000012
3    3.000012
4    4.000012
dtype: float64

回答by AlexG

You can plot nice histograms using the numpy timedelta data types.

您可以使用 numpy timedelta 数据类型绘制漂亮的直方图。

For example:

例如:

df['time_delta'].astype('timedelta64[s]').plot.hist()

will produce a histogram of the time deltas in seconds. To use minutes instead, you could do this:

将生成以秒为单位的时间增量直方图。要改用分钟,您可以这样做:

(df['time_delta'].astype('timedelta64[s]') / 60).plot.hist()

or use [m]timedelta.

或使用时间[m]增量。

df['time_delta'].astype('timedelta64[m]').plot.hist()

Here's list of other time delta types (from the docs) you might want, depending on the resolution you need:

以下是您可能需要的其他时间增量类型(来自docs)列表,具体取决于您需要的分辨率:

Code    Meaning Time span (relative)    Time span (absolute)
h   hour    +/- 1.0e15 years    [1.0e15 BC, 1.0e15 AD]
m   minute  +/- 1.7e13 years    [1.7e13 BC, 1.7e13 AD]
s   second  +/- 2.9e11 years    [2.9e11 BC, 2.9e11 AD]
ms  millisecond +/- 2.9e8 years [ 2.9e8 BC, 2.9e8 AD]
us  microsecond +/- 2.9e5 years [290301 BC, 294241 AD]
ns  nanosecond  +/- 292 years   [ 1678 AD, 2262 AD]
ps  picosecond  +/- 106 days    [ 1969 AD, 1970 AD]
fs  femtosecond +/- 2.6 hours   [ 1969 AD, 1970 AD]
as  attosecond  +/- 9.2 seconds [ 1969 AD, 1970 AD]