【问题标题】:Dividing a series containing datetime by a series containing an integer in Pandas在 Pandas 中将包含日期时间的系列除以包含整数的系列
【发布时间】:2013-08-09 22:19:16
【问题描述】:

我有一个系列 s1,它是 datetime 类型,并且有一个时间表示开始时间和结束时间之间的范围 - 典型值是 7 天、4 小时 5 分钟等。我有系列 s2,其中包含整数在该时间范围内发生的事件数。

我想通过以下方式计算事件频率:

event_freq = s1 / s2

我得到错误:

不能对没有 datetime64[ns] 类型的系列/ndarray 或 timedelta 的 rhs 的系列进行操作

解决这个问题的最佳方法是什么?

提前致谢!

s1 的示例是:

some_id

1          2012-09-02 09:18:40
3          2012-04-02 09:36:39
4          2012-02-02 09:58:02
5          2013-02-09 14:31:52
6          2012-01-09 12:59:20

s2 的例子是:

some_id
1           3
3           1
4           1
5           2
6           1
8           1
10          3
12          2

【问题讨论】:

  • 你能举一个 s1 和 s2 的例子来说明你的问题吗?
  • 嗨,安迪,刚刚添加 - 感谢您的提示。
  • s1 在一系列日期中,不确定将其除以 int 是否有意义?

标签: python pandas


【解决方案1】:

这可能是一个错误,但有效的是像这样对底层 numpy 数组进行操作:

import pandas as pd
from pandas import Series

startdate = Series(pd.date_range('2013-01-01', '2013-01-03'))
enddate = Series(pd.date_range('2013-03-01', '2013-03-03'))

s1 = enddate - startdate
s2 = Series([2, 3, 4])

event_freq = Series(s1.values / s2)

这是系列:

>>> s1
0   59 days, 00:00:00
1   59 days, 00:00:00
2   59 days, 00:00:00
dtype: timedelta64[ns]

>>> s2
0    2
1    3
2    4
dtype: int64

>>> event_freq
0   29 days, 12:00:00
1   19 days, 16:00:00
2   14 days, 18:00:00
dtype: timedelta64[ns]

【讨论】:

  • 这目前没有实现,主要是因为 numpy github.com/pydata/pandas/issues/4521
猜你喜欢
  • 1970-01-01
  • 2021-12-31
  • 2016-06-02
  • 2021-09-29
  • 2019-12-01
  • 2018-07-26
  • 2019-01-23
  • 2016-06-13
  • 2015-02-02
相关资源
最近更新 更多