我有一个大型数据库,我希望只阅读上周的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)]
我想以后忽略所有日期。我尝试过尝试:except:IndexError方法,但没有成功,因为IndexError标志只是在代码的后面抛出的。
我试过一个if循环
if df1['recvd_dttm'].max() > datetime.datetime.now():
但这些值是不可比较的,我不知道如何选择日期的倒数第二个值,因为max()-1显然不起作用。有人有什么想法吗?提前感谢!
您可以使用
mask = df1['recvd_dttm'] <= datetime.datetime.now()
df1 = df1.loc[mask]
以仅选择CCD_ 1小于当前日期时间的那些行。
我认为您的问题是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()]