【问题标题】:Cannot subtract one datetime column from another, subtract cannot use operands with types dtype('S1') and dtype('<M8[ns]')不能从另一列中减去一个日期时间列,减法不能使用类型为 dtype('S1') 和 dtype('<M8[ns]') 的操作数
【发布时间】:2019-06-23 06:00:19
【问题描述】:

我有两个 pandas 列,都转换为日期时间格式,不能从另一个中减去一个。

df['date_listed'] = pd.to_datetime(df['date_listed'], errors='coerce').dt.floor('d')
df['date_unconditional'] = pd.to_datetime(df['date_unconditional'], errors='coerce').dt.floor('d')

print df['date_listed'][:5]
print df['date_unconditional'][:5]

0   2013-01-01
1   2013-01-01
2   2015-04-08
3   2016-03-24
4   2016-04-27
Name: date_listed, dtype: datetime64[ns]
0   2018-10-15
1   2018-06-12
2   2018-08-28
3   2018-08-29
4   2018-10-29
Name: date_unconditional, dtype: datetime64[ns]

格式似乎是正确的,可以做减法,但后来我得到了这个错误:

df['date_listed_to_sale'] = (df['date_sold'] - df['date_listed']).dt.days
print df['date_listed_to_sale'][:5]



TypeErrorTraceback (most recent call last)
<ipython-input-139-85a5efbde0f1> in <module>()
----> 1 df['date_listed_to_sale'] = (df['date_sold'] - df['date_listed']).dt.days
      2 print df['date_listed_to_sale'][:5]

/Users/virt_env/virt1/lib/python2.7/site-packages/pandas/core/ops.pyc in wrapper(left, right)
   1581             rvalues = rvalues.values
   1582 
-> 1583         result = safe_na_op(lvalues, rvalues)
   1584         return construct_result(left, result,
   1585                                 index=left.index, name=res_name, dtype=None)

/Users/virt_env/virt1/lib/python2.7/site-packages/pandas/core/ops.pyc in safe_na_op(lvalues, rvalues)
   1531             if is_object_dtype(lvalues):
   1532                 return libalgos.arrmap_object(lvalues,
-> 1533                                               lambda x: op(x, rvalues))
   1534             raise
   1535 

pandas/_libs/algos.pyx in pandas._libs.algos.arrmap()

/Users/virt_env/virt1/lib/python2.7/site-packages/pandas/core/ops.pyc in <lambda>(x)
   1531             if is_object_dtype(lvalues):
   1532                 return libalgos.arrmap_object(lvalues,
-> 1533                                               lambda x: op(x, rvalues))
   1534             raise
   1535 

TypeError: ufunc subtract cannot use operands with types dtype('S1') and dtype('<M8[ns]')

我添加了 errors='coerce' 认为它可以解决问题,但它没有。我会很感激这方面的帮助。

【问题讨论】:

  • 错误表示“S1”中的第一个操作数 - 一个字节字符串字符,df['date_sold']

标签: python pandas datetime


【解决方案1】:

我认为您需要更改格式以获取时间戳的差异。

例如:

fmt = '%Y-%m-%d'
date_listed = datetime.datetime.strptime('2013-01-01', fmt)
date_unconditional = datetime.datetime.strptime('2018-10-15', fmt)
print("{0} years, {1} months, {2} days" .format((b.year-a.year),(b.month-a.month),(b.day-a.day)))

o/p:

5 years, 0 months, 4 days

【讨论】:

    【解决方案2】:

    如有必要,请更改减法的第一列:

    df['date_listed_to_sale'] = (df['date_unconditional'] - df['date_listed']).dt.days
    

    或将第一列 date_sold 转换为日期时间:

    df['date_sold'] = pd.to_datetime(df['date_sold'], errors='coerce').dt.floor('d')
    df['date_listed_to_sale'] = (df['date_sold'] - df['date_listed']).dt.days
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-23
      • 2020-12-28
      • 2020-08-29
      • 1970-01-01
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      相关资源
      最近更新 更多