【问题标题】:split-apply-combine on pandas timedelta columnpandas timedelta 列上的拆分应用组合
【发布时间】:2014-01-04 17:35:16
【问题描述】:

我有一个带有 timedeltas 列的 DataFrame(实际上在检查时 dtype 是 timedelta64[ns]<m8[ns]),我想做一个拆分组合应用,但是 timedelta 列正在被删除:

import pandas as pd

import numpy as np

pd.__version__
Out[3]: '0.13.0rc1'

np.__version__
Out[4]: '1.8.0'

data = pd.DataFrame(np.random.rand(10, 3), columns=['f1', 'f2', 'td'])

data['td'] *= 10000000

data['td'] = pd.Series(data['td'], dtype='<m8[ns]')

data
Out[8]: 
         f1        f2              td
0  0.990140  0.948313 00:00:00.003066
1  0.277125  0.993549 00:00:00.001443
2  0.016427  0.581129 00:00:00.009257
3  0.048662  0.512215 00:00:00.000702
4  0.846301  0.179160 00:00:00.000396
5  0.568323  0.419887 00:00:00.000266
6  0.328182  0.919897 00:00:00.006138
7  0.292882  0.213219 00:00:00.008876
8  0.623332  0.003409 00:00:00.000322
9  0.650436  0.844180 00:00:00.006873

[10 rows x 3 columns]

data.groupby(data.index < 5).mean()
Out[9]: 
             f1        f2
False  0.492631  0.480118
True   0.435731  0.642873

[2 rows x 2 columns]

或者,强制 pandas 尝试对 'td' 列的操作:

data.groupby(data.index < 5)['td'].mean()
---------------------------------------------------------------------------
DataError                                 Traceback (most recent call last)
<ipython-input-12-88cc94e534b7> in <module>()
----> 1 data.groupby(data.index < 5)['td'].mean()

/path/to/lib/python3.3/site-packages/pandas-0.13.0rc1-py3.3-linux-x86_64.egg/pandas/core/groupby.py in mean(self)
    417         """
    418         try:
--> 419             return self._cython_agg_general('mean')
    420         except GroupByError:
    421             raise

/path/to/lib/python3.3/site-packages/pandas-0.13.0rc1-py3.3-linux-x86_64.egg/pandas/core/groupby.py in _cython_agg_general(self, how, numeric_only)
    669 
    670         if len(output) == 0:
--> 671             raise DataError('No numeric types to aggregate')
    672 
    673         return self._wrap_aggregated_output(output, names)

DataError: No numeric types to aggregate

但是,取列的平均值可以正常工作,因此应该可以进行数字运算:

data['td'].mean()
Out[11]: 
0   00:00:00.003734
dtype: timedelta64[ns]

显然,在进行 groupby 之前强制浮动很容易,但我想我不妨尝试了解我遇到了什么。

编辑:见https://github.com/pydata/pandas/issues/5724

【问题讨论】:

  • 这是一个措辞出色的问题!您可以通过使用私有函数:data.groupby(data.index &lt; 5)._cython_agg_general('mean', numeric_only=False) 来解决这个问题,但您需要再次约会...我认为这应该是 feature request on github
  • 谢谢!不知道具体要求什么功能......熊猫应该至少尝试运行cython_agg_generalnumeric_only=False,因为有时它可以工作?
  • 那个 groupby mean 等应该识别并返回日期......我怀疑会有比使用 agg_general 这样的更优雅的实现......
  • 啊。我假设熊猫在后台使用 agg_general 并且仅使用数字 dtypes 调用它。但它可能比这更复杂。

标签: python pandas


【解决方案1】:

这对我有用:

data.groupby(data.index < 5)['td'].apply(lambda x: np.mean(x))

所以不要直接使用均值、中位数或任何用 lambda 函数包装它的东西。

不要问我为什么会这样。它只是没有打破我通常使用熊猫的方式。 所以我认为它比列表推导更加用户友好。 ;)

【讨论】:

    【解决方案2】:

    原来这是一个熊猫问题,这种行为needs to be implemented in groupby.py

    与此同时,请享受这种转换为浮动(以秒为单位)的解决方法:

    data['td'] = [10**-9 * float(td) for td in data['td']]
    

    【讨论】:

      猜你喜欢
      • 2014-09-10
      • 1970-01-01
      • 1970-01-01
      • 2015-02-02
      • 2022-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-25
      相关资源
      最近更新 更多