Python 具有日期值的元素最大值
我有一个带有日期值的数据框,并希望将其操作到1月1日或更晚。因为我需要按元素进行操作,所以我使用Python 具有日期值的元素最大值,python,pandas,numpy,datetime,Python,Pandas,Numpy,Datetime,我有一个带有日期值的数据框,并希望将其操作到1月1日或更晚。因为我需要按元素进行操作,所以我使用np.max()。然而,下面的代码给出了 TypeError:无法比较类型“Timestamp”和类型“int” 处理这种数据类型的合适方法是什么 import pandas as pd import numpy as np df = pd.DataFrame({'date': np.arange('1999-12', '2000-02', dtype='datetime64[D]')}) df['
np.max()
。然而,下面的代码给出了
TypeError:无法比较类型“Timestamp”和类型“int”
处理这种数据类型的合适方法是什么
import pandas as pd
import numpy as np
df = pd.DataFrame({'date': np.arange('1999-12', '2000-02', dtype='datetime64[D]')})
df['corrected_date'] = np.maximum(pd.to_datetime('20000101', format='%Y%m%d'), df['date'])
对于我来说,与
系列相比
:
s = pd.Series(pd.to_datetime('20000101', format='%Y%m%d'), index=df.index)
df['corrected_date'] = np.maximum(s, df['date'])
或使用DatetimeIndex
:
i = np.repeat(pd.to_datetime(['20000101'], format='%Y%m%d'), len(df))
df['corrected_date'] = np.maximum(i, df['date'])
对于我来说,与
系列相比
:
s = pd.Series(pd.to_datetime('20000101', format='%Y%m%d'), index=df.index)
df['corrected_date'] = np.maximum(s, df['date'])
或使用DatetimeIndex
:
i = np.repeat(pd.to_datetime(['20000101'], format='%Y%m%d'), len(df))
df['corrected_date'] = np.maximum(i, df['date'])
使用np.datetime64('2000-01-01',dtype='datetime64[D]')
进行比较?使用np.datetime64('2000-01-01',dtype='datetime64[D]')
进行比较?