我有一个大型数据库,我希望只阅读上周我的python代码。
然而,有人在数据库中输了一个拼写错误,因此将来有一个日期会把所有东西都扔掉。
输入:
recvd_dttm
6/5/2015 18:28:50 PM
6/5/2015 14:25:43 PM
9/10/2015 21:45:12 PM
6/5/2015 14:30:43 PM
6/5/2015 14:32:33 PM
6/5/2015 14:33:45 PM
到目前为止代码:
import datetime as datetime
#Create a dataframe with the data we are interested in
df1 =pd.read_csv('MYDATA.csv')
#This section selects the last week of data
# convert strings to datetimes
df1['recvd_dttm'] = pd.to_datetime(df1['recvd_dttm'])
# get first and last datetime for final week of data
range_max = df1['recvd_dttm'].max()
range_min = range_max - datetime.timedelta(days=7)
# take slice with final week of data
df2 = df1[(df1['recvd_dttm'] >= range_min) &
(df1['recvd_dttm'] <= range_max)]
我想忽略将来的所有日期。我尝试过尝试:除了:IndexError方法,但这没有用,因为IndexError标志只是稍后在代码中抛出。
我尝试过if循环
if df1['recvd_dttm'].max() > datetime.datetime.now():
但这些值无法比较,我不知道如何选择日期的倒数第二个值,因为max() - 1显然不起作用。有没有人有任何想法?提前谢谢!
答案 0 :(得分:1)
您可以使用
mask = df1['recvd_dttm'] <= datetime.datetime.now()
df1 = df1.loc[mask]
仅选择recvd_dttm
小于当前日期时间的行。
答案 1 :(得分:0)
我相信您的问题是to_datetime
并非像您期望的那样工作。您需要告诉它预期的具体日期格式。
import datetime as datetime
import pandas as pd
# prepare the dataframe
dates = ['6/5/2015 18:28:50 PM', '6/5/2015 14:25:43 PM', '9/10/2015 21:45:12 PM', '6/5/2015 14:30:43 PM', '6/5/2015 14:32:33 PM', '6/5/2015 14:33:45 PM']
df1 = pd.DataFrame({"recvd_dttm": dates})
# properly convert dates
df1['recvd_dttm'] = pd.to_datetime(df1['recvd_dttm'], format='%m/%d/%Y %H:%M:%S %p')
# drop rows with dates in the future
df1 = df1[df1['recvd_dttm'] < datetime.datetime.now()]