基本上,unicode格式将从一个列的datepicker和25-06-15 08:42:43.830000000 PM
这种格式中获取
我的数据框是:
query,status,received_date
a,closed,25-06-15 08:42:43.830000000 PM
b,pending,27-06-15 08:42:43.830000000 PM
ab,closed,28-06-15 08:42:43.830000000 PM
bb,pending,29-06-15 08:42:43.830000000 PM
我将从datepicker获得两个日期,例如以下格式(u'2015-06-23',u'2015-06-29')
。如何比较这个unicode日期和recieved_date列。
我必须在这两个日期之间显示数据(将从datepicker获得)
答案 0 :(得分:1)
将它们转换为日期时间。
{{1}}
答案 1 :(得分:1)
我认为您首先需要转换dates
to_datetime
,然后转换received_date
列并提取date
。上次使用boolean indexing
与mask
进行过滤:
#datetimes changed for better testing
print df
query status received_date
0 a closed 20-06-15 08:42:43.830000000 PM
1 b pending 27-06-15 08:42:43.830000000 PM
2 ab closed 28-06-15 08:42:43.830000000 PM
3 bb pending 30-06-15 08:42:43.830000000 PM
dates = (u'2015-06-23',u'2015-06-29')
dates = pd.to_datetime(dates).date
print dates
[datetime.date(2015, 6, 23) datetime.date(2015, 6, 29)]
df['received_date'] = pd.to_datetime(df['received_date']).dt.date
print df
query status received_date
0 a closed 2015-06-20
1 b pending 2015-06-27
2 ab closed 2015-06-28
3 bb pending 2015-06-30
print (df['received_date'] > dates[0]) & (df['received_date'] < dates[1])
0 False
1 True
2 True
3 False
Name: received_date, dtype: bool
df = df[(df['received_date'] > dates[0]) & (df['received_date'] < dates[1])]
print df
query status received_date
1 b pending 2015-06-27
2 ab closed 2015-06-28
但更快的修改PhilChang
解决方案:
dates = (u'2015-06-23',u'2015-06-29')
df['received_date'] = pd.to_datetime(df['received_date'])
df = df.set_index('received_date')
return df[dates[0]:dates[1]]
测试(len(df) == 40k
):
In [569]: %timeit a(df)
1 loops, best of 3: 12.2 s per loop
In [570]: %timeit b(df1)
10 loops, best of 3: 92.3 ms per loop
In [571]: %timeit c(df2)
100 loops, best of 3: 6.57 ms per loop
测试代码:
#length is 40k
df = pd.concat([df]*10000).reset_index(drop=True)
df1 = df.copy()
df2 = df.copy()
def a(df):
dates = (u'2015-06-23',u'2015-06-29')
df = df.set_index('received_date')
df.index = pd.DatetimeIndex(df.index)
return df[dates[0]:dates[1]]
def b(df):
dates = (u'2015-06-23',u'2015-06-29')
dates = pd.to_datetime(dates).date
df['received_date'] = pd.to_datetime(df['received_date']).dt.date
df = df[(df['received_date'] > dates[0]) & (df['received_date'] < dates[1])]
return df
def c(df):
dates = (u'2015-06-23',u'2015-06-29')
df['received_date'] = pd.to_datetime(df['received_date'])
df = df.set_index('received_date')
return df[dates[0]:dates[1]]
print a(df)
print b(df1)
print c(df2)