【问题标题】:Pandas Sort by DatePandas 按日期排序
【发布时间】:2020-12-06 02:00:05
【问题描述】:

我目前正在尝试对 pandas DataFrame 进行排序,但数据类型出现了奇怪的错误。我相信 .to_datetime 将对象转换为 numpy datetime 对象,因此我目前正在尝试使用该数据类型进行排序。这是抛出的错误。

result = op(x, y)
TypeError: unsupported operand type(s) for &: 'numpy.ndarray' and 'DatetimeArray'

代码:

df['quote_datetime'] = pd.to_datetime(df['quote_datetime'], format='%Y-%m-%d %H:%M:%S.%f', errors='coerce')

print(df[(df['quote_datetime'] > np.datetime64('2020-08-07T09:30')) & df['quote_datetime'] < np.datetime64('2020-08-07T10:00')])

【问题讨论】:

    标签: python pandas numpy sorting datetime


    【解决方案1】:

    您还需要将第二个条件放在括号中:

    print(df[(df['quote_datetime'] > np.datetime64('2020-08-07T09:30')) & (df['quote_datetime'] < np.datetime64('2020-08-07T10:00'))])
    

    【讨论】:

      猜你喜欢
      • 2022-08-11
      • 1970-01-01
      • 2016-06-10
      • 2011-02-19
      • 1970-01-01
      • 2017-01-19
      • 2017-07-05
      • 2016-07-26
      • 2016-10-22
      相关资源
      最近更新 更多