我想问一下,如何在datetime中制作范围。我已经尝试制作范围,但我没有工作。我的代码如下:
import pandas
import os
import datetime
import numpy as np
import glob
def convert_stamp_to_date(stamp):
try:
d = datetime.datetime.utcfromtimestamp(stamp)
except:
d = datetime.datetime.utcfromtimestamp(0)
d = datetime.datetime(d.year, d.month, d.day, d.hour, d.minute, d.second)
return d
activity_df = pandas.read_csv('D:/Ipython/PersonalDataProject/ITRCProject/oneP/activity.csv', index_col=0)
activity_df['timestamp'] = activity_df['timestamp'].apply(convert_stamp_to_date)
activity_df = activity_df.drop_duplicates('timestamp')
activity_df = activity_df['2014-06-28 00:00:00'<activity_df['timestamp']<'2014-06-29 00:00:00']
activity_df.columns = ['timestamp', 'activity']
ValueError Traceback(最近一次调用 最后)in() 17 activity_df [&#39; timestamp&#39;] = activity_df [&#39; timestamp&#39;]。apply(convert_stamp_to_date) 18 activity_df = activity_df.drop_duplicates(&#39; timestamp&#39;) ---&GT; 19 activity_df = activity_df [&#39; 2014-06-28 00:00:00&#39;
C:\ Anaconda \ lib \ site-packages \ pandas \ core \ generic.pyc in 的非零(个体) 712引发ValueError(&#34; {0}的真值是不明确的。&#34; 713&#34;使用a.empty,a.bool(),a.item(),a.any()或a.all()。&#34; - &GT; 714 .format(self。 class 。 name )) 715 716 bool = 非零
ValueError:系列的真值是不明确的。使用a.empty, a.bool(),a.item(),a.any()或a.all()。
答案 0 :(得分:1)
import numpy as np
import datetime as dtm
date1 = dtm.datetime(2014,6,28,0,0,0)
date2 = dtm.datetime(2014,6,29,0,0,0)
#condition = np.logical_and(activity_df>date1,activity_df<date2)
#correction
np.logical_and(activity_df['timestamp']>date1,activity_df['timestamp']<date2)
activity_df = activity_df[condition]