按日期过滤熊猫数据框不起作用

问题描述 投票:0回答:1

我正在使用Cryptowatch API下载比特币价格数据。下载价格数据的效果很好,但是我只需要1个月之前的价格数据,因此数据从2019年10月29日到2019年11月28日。我阅读了几个类似问题的答案,但它似乎不适用于我的代码,因为过滤后与未过滤时得到相同的输出。

这是我的代码:

#define 1day period
periods = '86400'

#get price data from cryptowatchAPI

resp = requests.get('https://api.cryptowat.ch/markets/bitfinex/btcusd/ohlc', params={'periods': periods})

resp.ok

#create pandas dataframe
data = resp.json()
df = pd.DataFrame(data['result'][periods], columns=[
    'CloseTime', 'OpenPrice', 'HighPrice', 'LowPrice', 'ClosePrice', 'Volume', 'NA'])

#Make a date out of CloseTime
df['CloseTime'] = pd.to_datetime(df['CloseTime'], unit='s')


#make CloseTime Index of the Dataframe
df.set_index('CloseTime', inplace=True)

#filter df by date until 1 month ago
df.loc[datetime.date(year=2019,month=10,day=29):datetime.date(year=2019,month=11,day=28)]

df

没有错误或任何东西,但是输出始终相同,因此过滤不起作用。预先非常感谢!

python pandas dataframe filtering bitcoin
1个回答
0
投票

使用日期时间的string格式进行过滤:

df1 = df['2019-10-29':'2019-11-28']

或:

s = datetime.datetime(year=2019,month=10,day=29)
e = datetime.datetime(year=2019,month=11,day=28)
df1 = df[s:e]
© www.soinside.com 2019 - 2024. All rights reserved.