【问题标题】:Filtering Pandas Dataframe by date not working按日期过滤熊猫数据框不起作用
【发布时间】:2019-11-28 17:45:01
【问题描述】:

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

这是我的代码:

#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】:

    使用strings 格式的日期时间进行过滤,更多信息请查看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]
    

    【讨论】:

      猜你喜欢
      • 2018-01-18
      • 2020-05-05
      • 2013-06-09
      • 2020-03-19
      • 1970-01-01
      • 2017-05-10
      • 2019-04-13
      • 2021-08-19
      • 1970-01-01
      相关资源
      最近更新 更多