首页 > 解决方案 > 使用 pandas groupby 检查预订日期和关闭日期的重叠

问题描述

overlap = df.groupby('CompanyName')

for g_idx, group in overlap:
  for r_idx, row in group.iterrows():
    if (row['ClosedDate'] - group['BookingDate'].any() > timedelta(0)):
      df.loc[r_idx, 'overlap'] = True
    else:
      df.loc[r_idx, 'overlap'] = False

我收到此错误

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-111-52f6bd79e6d1> in <module>()
      1 for g_idx, group in overlap:
      2   for r_idx, row in group.iterrows():
----> 3     if (row['ClosedDate'] - group['BookingDate'].any() > timedelta(0)):
      4       df.loc[r_idx, 'overlap'] = True
      5     else:

3 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/arrays/base.py in _reduce(self, name, skipna, **kwargs)
   1145         TypeError : subclass does not define reductions
   1146         """
-> 1147         raise TypeError(f"cannot perform {name} with type {self.dtype}")
   1148 
   1149     def __hash__(self):

TypeError: cannot perform any with type datetime64[ns, UTC]

标签: pythonpandaspandas-groupby

解决方案


推荐阅读