Python 3.x 在python中,从两个时间戳之间选择数据
我的查询正在对获取数据进行重新分级,在python中给出了两个时间戳 我需要有一个输入字段,在这里我可以输入两个时间戳,然后从CSV读取,我需要检索该特定输入 Actaul数据(CSV) 例如:Python 3.x 在python中,从两个时间戳之间选择数据,python-3.x,pandas,datetime,dataframe,timestamp,Python 3.x,Pandas,Datetime,Dataframe,Timestamp,我的查询正在对获取数据进行重新分级,在python中给出了两个时间戳 我需要有一个输入字段,在这里我可以输入两个时间戳,然后从CSV读取,我需要检索该特定输入 Actaul数据(CSV) 例如: Input: start date : 2-1-2017 end date :10-1-2017 输出 Timestamp Value 2-1-2017 10 3-1-2017 35 . . . . 10-1-2017 25 原始CSV将包含所有数据 Timest
Input:
start date : 2-1-2017
end date :10-1-2017
输出
Timestamp Value
2-1-2017 10
3-1-2017 35
.
.
.
.
10-1-2017 25
原始CSV将包含所有数据
Timestamp Value
1-12-2016 10
2-12-2016 25
.
.
.
1-1-2017 15
2-1-2017 10
.
.
.
10-1-2017 25
.
.
31-1-2017 50
使用
pd.read\u csv
读取文件
df = pd.read_csv('my.csv', index_col='Timestamp', parse_dates=[0])
然后使用您的输入进行切片
df[start_date:end_date]
如果所有开始和结束日期都在
df.index
中,您似乎需要dayfirst=True
和select by[]
:
import pandas as pd
from pandas.compat import StringIO
temp=u"""Timestamp;Value
1-12-2016;10
2-12-2016;25
1-1-2017;15
2-1-2017;10
10-1-2017;25
31-1-2017;50"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
#if necessary add sep
#index_col=[0] convert first column to index
#parse_dates=[0] parse first column to datetime
df = pd.read_csv(StringIO(temp), sep=";", index_col=[0], parse_dates=[0], dayfirst=True)
print (df)
Value
Timestamp
2016-12-01 10
2016-12-02 25
2017-01-01 15
2017-01-02 10
2017-01-10 25
2017-01-31 50
print (df.index.dtype)
datetime64[ns]
print (df.index)
DatetimeIndex(['2016-12-01', '2016-12-02', '2017-01-01', '2017-01-02',
'2017-01-10', '2017-01-31'],
dtype='datetime64[ns]', name='Timestamp', freq=None)
如果某些日期不在索引中,您需要:
时间戳的格式为“26/09/2016 23:58”。因此,它抛出了一个关键错误。KeyError:Timestamp('2016-12-02 00:00:00')
print(df[start\u date:end\u date])
或print(df[(df.index>start\u date)&(df.index>end\u date)]
?如果第一个和数据在索引中,请尝试打印(df[str(start\u date):str(end\u date)])
好,返回什么打印(df.index.dtype)
?是的,我正在尝试更改它的日期类型。由于其抛出错误“无法将类型‘Timestamp’与类型‘str’进行比较”print(df.index.dtype)=这表示df是一个对象,时间戳的格式为“26/09/2016 23:58”。因此,它抛出了一个关键错误。KeyError:时间戳('2016-12-02 00:00:00')
import pandas as pd
from pandas.compat import StringIO
temp=u"""Timestamp;Value
1-12-2016;10
2-12-2016;25
1-1-2017;15
2-1-2017;10
10-1-2017;25
31-1-2017;50"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
#if necessary add sep
#index_col=[0] convert first column to index
#parse_dates=[0] parse first column to datetime
df = pd.read_csv(StringIO(temp), sep=";", index_col=[0], parse_dates=[0], dayfirst=True)
print (df)
Value
Timestamp
2016-12-01 10
2016-12-02 25
2017-01-01 15
2017-01-02 10
2017-01-10 25
2017-01-31 50
print (df.index.dtype)
datetime64[ns]
print (df.index)
DatetimeIndex(['2016-12-01', '2016-12-02', '2017-01-01', '2017-01-02',
'2017-01-10', '2017-01-31'],
dtype='datetime64[ns]', name='Timestamp', freq=None)
start_date = pd.to_datetime('2-1-2017', dayfirst=True)
end_date = pd.to_datetime('10-1-2017', dayfirst=True)
print (df[start_date:end_date])
Value
Timestamp
2017-01-02 10
2017-01-10 25
start_date = pd.to_datetime('3-1-2017', dayfirst=True)
end_date = pd.to_datetime('10-1-2017', dayfirst=True)
print (df[(df.index > start_date) & (df.index > end_date)])
Value
Timestamp
2017-01-31 50