如果行中的日期大于或等于 80 天,则尝试从 csv_file 中删除行。
这是 CSV_FILE:(所有内容都被读取并设置为 CSV_FILE 中的字符串)
2019-05-01 | 14
2019-05-02 | 16
2019-05-03 | 2
2019-05-04 | 3
2019-05-05 | 3
2019-05-06 | 6
2019-05-07 | 14
2019-05-08 | 8
2019-05-09 | 5
2019-05-10 | 1
2019-05-11 | 5
2019-05-12 | 4
2019-05-13 | 1
2019-05-14 | 2
2019-05-15 | 3
2019-05-16 | 8
2019-05-17 | 2
2019-05-18 | 3
2019-05-19 | 4
2019-05-20 | 4
这是我尝试过的:
s = pd.Series(pd.to_datetime('today') - pd.to_datetime(df.index[0])).dt.days df[s.le(80)].reset_index(drop=True).to_csv(csv_file, index=False)
不工作,因为 pd.Series 失败了我想做的事。我目前正在做的是尝试通过 df.drop() 就地,但我不知道出了什么问题并抛出错误。
逻辑实现的工作,但它返回错误KeyError: True或KeyError: False基于 [0] 索引比较布尔逻辑。
df = pd.read_csv(GLOBAL_PATH + csv_file, sep=',', index_col=0, encoding='utf-8', low_memory=False)
# print(df)
df.drop(df[(pd.to_datetime('today') - pd.to_datetime(df.index[0])).days >= 82].index, inplace=True)
如果第一个索引中的日期大于或等于 80 天,我试图从 csv_file 中永久删除行。
任何帮助表示赞赏!谢谢!
- 编辑 -
对于任何仍在寻找的人。Ian Thompson 确实回答了这个问题,这就是我正在做的最终代码的样子(顶部的工作代码)。我还包含了我一直在解决这个问题的所有其他代码,以防它在将来对其他人有所帮助。
def remove_old_data(csv_file):
# WORKING CODE
df = pd.read_csv(GLOBAL_PATH + csv_file, sep=',', index_col=0, encoding='utf-8', low_memory=False)
# print(df) # Before Removal
df.drop(df.loc[(pd.to_datetime('today') - pd.to_datetime(df.index)).days >= 180].index, inplace=True)
# print(df) # After Removal
# Appended to CSV_FILE
df.to_csv(GLOBAL_PATH + csv_file)
# TEST OUT CODE
s1 = (pd.to_datetime('today') - pd.to_datetime(df.index)).days
print(s1, type(s1)) # Int64Index([84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65],
# dtype='int64', name='date') <class 'pandas.core.indexes.numeric.Int64Index'>
s2 = (pd.to_datetime('today') - pd.to_datetime(df.index[0])).days # Calculate the date difference
print(s2, type(s2)) # 82 <class 'int'>
zeroindex = df.index[0]
print(zeroindex, type(zeroindex)) # 2019-05-01 <class 'str'>
datestamp = pd.to_datetime(df.index[0])
print(datestamp, type(datestamp)) # 2019-05-01 00:00:00 <class 'pandas._libs.tslibs.timestamps.Timestamp'>
print(df.loc[s1 <= 80])
print(df.loc[(pd.to_datetime('today') - pd.to_datetime(df.index)).days <= 80])
# TEST DROP CODE
# df.drop(df[(pd.to_datetime('today') - pd.to_datetime(df.index[0])).days >= 82].index, inplace=True)
# df.drop(df[df.iloc[[0]].le((pd.to_datetime('today') - pd.to_datetime(df.index[0])).days >= 90)].index, inplace=True)
# NONE WORKING CODE / IN PROGRESS CODE
# Just days time == (pd.to_datetime('today') - pd.to_datetime(df.index[0])).days
# s = pd.Series(pd.to_datetime('today') - pd.to_datetime(df.index[0])).dt.days # Calculate the date difference
# print(s[0], type(s[0]), type(s)) # Result -- 57 <class 'numpy.int64'> <class 'pandas.core.series.Series'>
# df[s.le(55)]#.reset_index(drop=True).to_csv(csv_file, index=False)
# df2 = df.drop(axis=0, index=df.index[0], inplace=False).reset_index(drop=True).to_csv(csv_file, index=False)
# df2 = df.loc[df.index[0]].le(40).reset_index(drop=False)#.to_csv(csv_file, index=False, header=False, sep=',')