【问题标题】:Convert pandas unicode column to timestamp python将pandas unicode列转换为时间戳python
【发布时间】:2018-10-06 17:10:39
【问题描述】:

我得到:

TypeError: Cannot compare type 'Timestamp' with type 'unicode'

当我尝试这样做时:

df = df[df['timestamp'] >= start]
df = df[df['timestamp'] < (end + timedelta(days=1))]

数据类型有:

type(df['timestamp'][0])
Out[134]: unicode
type(start)
Out[135]: pandas._libs.tslib.Timestamp

所以,我一直在尝试将列转换为 pandas 日期时间。我做了以下,它仍然是一个 unicode 数据类型。

df['timestamp'] = pd.to_datetime(df['timestamp'], format='%Y-%m-%d %H:%m:%s', errors='coerce')

【问题讨论】:

标签: python pandas datetime unicode


【解决方案1】:

错误提示:

TypeError: Cannot compare type 'Timestamp' with type 'unicode'

这意味着您进行了比较,左侧是Timestamp,右侧是unicode(假设错误当然发生在比较级别)。

由于错误发生在:

df = df[df['timestamp'] >= start]

这意味着start 是unicode 字符串,而不是时间戳。

因此您首先需要转换start,例如:

df = df[df['timestamp'] &gt;= <b>pd.to_datetime(</b>start<b>)</b>]

【讨论】:

  • 正是我所做的。感谢您解释/回答
猜你喜欢
  • 2018-10-09
  • 2016-10-05
  • 2023-01-25
  • 2016-03-21
  • 1970-01-01
  • 2014-12-17
  • 2021-01-18
  • 2014-07-13
  • 1970-01-01
相关资源
最近更新 更多