Python将时间戳与输入时间进行比较
我有一些带有时间戳的数据帧作为列,我想用np.where过滤8:00:00到17:00:00之间的行。我不断收到关于数据/对象类型的错误消息。任何帮助都将不胜感激 例如:Python将时间戳与输入时间进行比较,python,pandas,dataframe,timestamp,Python,Pandas,Dataframe,Timestamp,我有一些带有时间戳的数据帧作为列,我想用np.where过滤8:00:00到17:00:00之间的行。我不断收到关于数据/对象类型的错误消息。任何帮助都将不胜感激 例如: timestamp volume 2013-03-01 07:59:00 5 2013-03-01 08:00:00 6 2013-03-01 08:01:00 7 2013-03-01 08:02:00 8 基本上,我想以以下内容结束: 2013-03-01 08:00:00 6 201
timestamp volume
2013-03-01 07:59:00 5
2013-03-01 08:00:00 6
2013-03-01 08:01:00 7
2013-03-01 08:02:00 8
基本上,我想以以下内容结束:
2013-03-01 08:00:00 6
2013-03-01 08:01:00 7
2013-03-01 08:02:00 8
通过使用沿着
np.where(df['timestamp'] > dt.time('8:00:00')
试试这个:
In [226]: df
Out[226]:
timestamp volume
0 2013-03-01 07:59:00 5
1 2013-03-01 08:00:00 6
2 2013-03-01 08:01:00 7
3 2013-03-01 08:02:00 8
In [227]: df.dtypes
Out[227]:
timestamp object
volume int64
dtype: object
In [228]: df['timestamp'] = pd.to_datetime(df['timestamp'], errors='coerce')
In [229]: df.dtypes
Out[229]:
timestamp datetime64[ns] # <---- it's `datetime64[ns]` now
volume int64
dtype: object
In [230]: df.set_index('timestamp').between_time('08:00','17:00').reset_index()
Out[230]:
timestamp volume
0 2013-03-01 08:00:00 6
1 2013-03-01 08:01:00 7
2 2013-03-01 08:02:00 8
[226]中的:df
出[226]:
时间戳卷
0 2013-03-01 07:59:00 5
1 2013-03-01 08:00:00 6
2 2013-03-01 08:01:00 7
3 2013-03-01 08:02:00 8
在[227]中:df.dtypes
出[227]:
时间戳对象
卷int64
数据类型:对象
在[228]中:df['timestamp']=pd.to_datetime(df['timestamp'],errors='procure')
In[229]:df.dtypes
出[229]:
时间戳datetime64[ns]#您可以在
我生成了一个带有
import datetime
d = {'timestamp': pd.Series([datetime.datetime.now() +
datetime.timedelta(hours=i) for i in range(20)]),
'volume': pd.Series([s for s in range(20)])}
df = pd.DataFrame(d)
df['timeframe']
0 2017-02-13 22:37:54.515840
1 2017-02-13 23:37:54.515859
2 2017-02-14 00:37:54.515865
3 2017-02-14 01:37:54.515870
4 2017-02-14 02:37:54.515878
5 2017-02-14 03:37:54.515884
6 2017-02-14 04:37:54.515888
...
17 2017-02-14 15:37:54.515939
18 2017-02-14 16:37:54.515943
19 2017-02-14 17:37:54.515948
df.dtypes
timestamp datetime64[ns]
volume int64
dtype: object
在您的示例中,df['timestamp']
的dtype
是object
您可以执行的操作
df['timestamp'] = pd.to_datetime(df['timestamp'], coerce=True)
通过设置paramconcurve=True
如果任何特定字符串的转换失败,则这些行被设置为NaT
然后可以使用在之间进行过滤,如下所示
df[df.timestamp.dt.strftime(“%H:%M:%S”)。介于('11:00:00'、'18:00:00')之间]
会回来的
13 2017-02-14 11:37:54.515922 13
14 2017-02-14 12:37:54.515926 14
15 2017-02-14 13:37:54.515930 15
16 2017-02-14 14:37:54.515935 16
17 2017-02-14 15:37:54.515939 17
18 2017-02-14 16:37:54.515943 18
19 2017-02-14 17:37:54.515948 19
如果您有一个包含以下数据的文件:
时间戳卷
2013-03-01 07:59:00 5
2013-03-01 08:00:00 6
2013-03-01 08:01:00 7
2013-03-01 08:02:00 8
import pandas as pd
df=pd.read_csv("filename",skiprows=1)
print(df)
然后,当只阅读时,您可以跳过第一行,您将得到如下输出:
时间戳卷
2013-03-01 08:00:00 6
2013-03-01 08:01:00 7
2013-03-01 08:02:00 8
import pandas as pd
df=pd.read_csv("filename",skiprows=1)
print(df)
张贴您收到的错误消息:它们经常会告诉您您做错了什么。什么是df['timestamp'].dtpye
?如果第二行应该进入结果集中,您需要使用=
。另外,df[df.timestamp>'08:00:00']
无论数据类型是object还是datetime都应该有效。df.set_index('timestamp')。在_-time('08:00','17:00')之间。reset_-index()
?df['timestamp']。dtype显示数据类型('O')。我尝试了df[df.timestamp>'08:00:00'],但没有运气-表返回空数据帧np。其中(df['timestamp']>dt.time('8:00:00')产生:TypeError:descriptor'time'需要一个'datetime.datetime'对象,但收到了一个'str',它不起作用,因为我的timestamp dtype是对象,而不是datetime64[ns].Get错误:只能使用带有datetimelike值的.dt访问器。请尝试df['date time']=pd.to_datetime(df['timestamp'],强制=True)
在筛选之前?我可以运行此操作,直到我查看结果透视表并看到8:00:00之前的时间。我的数据包含多天,因此我想知道这是否可行?我将结果数据打印到csv中,并且仍然看到此时间段之外的时间戳。我的错误是,忘记将数据框设置为更新。这很有效。