gpt4 book ai didi

python - 按日期过滤 Pandas 数据框不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 23:51:55 25 4
gpt4 key购买 nike

我正在使用 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

没有错误什么的,但是输出总是一样的,所以过滤不起作用。非常感谢您!!

最佳答案

使用 string 格式的日期时间进行过滤,更多信息请查看 docs :

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]

关于python - 按日期过滤 Pandas 数据框不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59085663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com