简体   繁体   English

类型错误:+ 不支持的操作数类型:“datetime.datetime”和“datetime.time”

[英]TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 'datetime.time'

I have a pandas DataFrame:我有一个 pandas DataFrame:

In [33]: df
Out[33]:
                         userId          2021-01-29          2021-01-30          2021-01-01
0                Nl3AG93Ss7L5aj            09:00:00                 NaN                 NaN
1                           NaN                 NaN                 NaN                 NaN
2                AbVpBHdfrI5aj1            12:10:00                 NaN                 NaN
3                           NaN                 NaN                 NaN                 NaN
4                           NaN                 NaN                 NaN                 NaN
5               sad9283ds7L5aj1                 NaN            15:35:00            22:22:00
6                           NaN                 NaN                 NaN                 NaN
7                           NaN                 NaN                 NaN                 NaN
8                           NaN                 NaN                 NaN                 NaN

I need to get the date and time the script started working, but I get the error:我需要获取脚本开始工作的日期和时间,但出现错误:

TypeError Traceback (most recent call last)
<ipython-input-40-7ed51ff0c115> in <module>
      1 for column in df.columns.tolist():
----> 2     for  i in df.loc[df[column].apply(lambda x: isinstance(x, datetime.time))][column]: print(column + i)
      3

TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 'datetime.time'

Also I can output the schedule without merging:我也可以 output 计划而不合并:

In [22]: for column in df.columns.tolist():
    ...:     for  time in df.loc[df[column].apply(lambda x: isinstance(x, datetime.time))][column]: print(column, time)
    ...:
2021-01-29 00:00:00 09:00:00
2021-01-29 00:00:00 12:10:00
2021-01-30 00:00:00 15:35:00
2021-01-01 00:00:00 22:22:00

Try converting the things into strings and just using the + as string concat...尝试将内容转换为字符串,然后将+用作字符串连接...

for column in df.columns.tolist():
    for  i in df.loc[df[column].apply(lambda x: isinstance(x, datetime.time))][column]: print(column.strftime("%Y-%m-%d”) +" "+ i.strftime("%H:%M:%S”))
      

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 - 不支持的操作数类型:“datetime.time”和“datetime.time” - unsupported operand type(s) for -: 'datetime.time' and 'datetime.time' TypeError: 不支持的操作数类型 +: &#39;Timedelta&#39; 和 &#39;datetime.time - python - TypeError: unsupported operand type(s) for +: 'Timedelta' and 'datetime.time - python TypeError:不支持的操作数类型 - :&#39;datetime.time&#39;和&#39;datetime.time&#39; - TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time' TypeError:不支持的操作数类型 -:&#39;datetime.time&#39; 和 &#39;datetime.time&#39; 站点:stackoverflow.com - TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time' site:stackoverflow.com TypeError:-:“ datetime.datetime”和“ int”的不受支持的操作数类型 - TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'int' TypeError:&amp;:'datetime.datetime'和'bool'不支持的操作数类型 - TypeError: unsupported operand type(s) for &: 'datetime.datetime' and 'bool' TypeError:+不支持的操作数类型:“ datetime.datetime”和“ str” - TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 'str' TypeError: 不支持的操作数类型 -: 'datetime.datetime' 和 'str' - TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str' PySpark:TypeError:+不支持的操作数类型:&#39;datetime.datetime&#39;和&#39;str&#39; - PySpark: TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 'str' datetime.datetime对象的总和给出了错误TypeError:+:&#39;datetime.datetime&#39;和&#39;datetime.datetime&#39;的不支持的操作数类型 - sum of datetime.datetime object gave an error TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 'datetime.datetime'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM