【问题标题】:Python Pandas calculate time until output reaches 0Python Pandas 计算输出达到 0 的时间
【发布时间】:2017-01-27 05:19:03
【问题描述】:

我在 python 中有一个带有几列和一个日期时间戳的 pandas 数据框。其中一列有一个真/假变量。我想计算该列为假之前的时间。

理想情况下应该是这样的:

datetime             delivered    secondsuntilfailure
2014-05-01 01:00:00    True       3
2014-05-01 01:00:01    True       2
2014-05-01 01:00:02    True       1
2014-05-01 01:00:03    False      0
2014-05-01 01:00:04    True       ?

提前致谢!!

【问题讨论】:

  • 你试过了吗?你能告诉我们你的代码吗?

标签: python datetime pandas


【解决方案1】:

您可以先通过[::-1]更改订单,然后找到diff,如果值为cumsum,则计数True

df = df[::-1]
print (df.datetime.diff().astype('timedelta64[s]'))
4    NaN
3   -1.0
2   -1.0
1   -1.0
0   -1.0
Name: datetime, dtype: float64

df['new'] = df.delivered.where(~df.delivered,df.datetime.diff().astype('timedelta64[s]'))
              .cumsum().fillna(0).astype(int).mul(-1)
df = df[::-1]
print (df)
             datetime delivered secondsuntilfailure  new
0 2014-05-01 01:00:00      True                   3    3
1 2014-05-01 01:00:01      True                   2    2
2 2014-05-01 01:00:02      True                   1    1
3 2014-05-01 01:00:03     False                   0    0
4 2014-05-01 01:00:04      True                   ?    0

【讨论】:

  • 我收到以下错误:TypeError Traceback (last last call last) in () 4 5 ----> 6 df['new' ] = df.Pffr_deliverysuccess.where(~df.Pffr_deliverysuccess,df.DateTime.diff().astype('timedelta64[s]')).cumsum().fillna(0).astype(int).mul(-1 ) 7 df = df[::-1] 8 打印 (df)
  • C:\Users\caljrr\Anaconda2\lib\site-packages\pandas\core\generic.pyc in __invert__(self) 780 def __invert__(self): 781 try: --> 782 arr = operator.inv(_values_from_object(self)) 783 return self.__array_wrap__(arr) 784 except: TypeError: bad operand type for unary ~: 'float'
  • 列名其实是Pffr_deliverysuccess not Deliver
  • 对不起,我想~df.Pffr_deliverysuccess
【解决方案2】:

数秒:

cumsecs = df.datetime.diff().astype('timedelta64[s]').cumsum().fillna(value=0.0)

每当交付失败时复制累积值,并填写任何前面的值:

undeliv_secs = cumsecs.where(~df['delivered']).fillna(method='bfill')

直到失败的秒数只是两者之间的区别:

df['secondsuntilfailure'] = undeliv_secs - cumsecs
print(df)
             datetime delivered  secondsuntilfailure
0 2014-05-01 01:00:00      True                  3.0
1 2014-05-01 01:00:01      True                  2.0
2 2014-05-01 01:00:02      True                  1.0
3 2014-05-01 01:00:03     False                  0.0
4 2014-05-01 01:00:04      True                  NaN

【讨论】:

  • 我收到以下错误:ypeError Traceback (最近一次调用最后一次) in () 1 2 cumsecs = df.DateTime.diff().astype ('timedelta64[s]').cumsum().fillna(value=0.0) ----> 3 undeliv_secs = cumsecs.where(~df['Pffr_deliverysuccess']).fillna(method='bfill') 4 df ['持续时间'] = undeliv_secs - cumsecs
  • C:\Users\caljrr\Anaconda2\lib\site-packages\pandas\core\generic.pyc in __invert__(self) 780 def __invert__(self): 781 try: --> 782 arr = operator.inv(_values_from_object(self)) 783 return self.__array_wrap__(arr) 784 except: TypeError: bad operand type for unary ~: 'float'
  • 听起来你的 Pffr_deliverysuccess 是一个浮动。您应该创建一个新列,将其转换为 bool 并在 where() 中使用
猜你喜欢
  • 2021-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-04
  • 2016-12-11
  • 2017-09-02
  • 2013-01-21
  • 2015-04-30
相关资源
最近更新 更多