![](/img/trans.png)
[英]Pandas: resample a dataframe to match a DatetimeIndex of a different dataframe
[英]Resample Pandas Dataframe based on a DatetimeIndex variable
我有一個帶有日期索引的 dataframe:
Symbol Shares Price Commission
Date
2017-12-06 BNP 0 0 10.0
2018-10-09 BNP 0 0 10.0
和一個單獨的 DatetimeIndex 變量:
DatetimeIndex(['2014-02-14', '2014-02-15', '2014-02-16', '2014-02-17',
...
'2020-04-11', '2020-04-12'],
dtype='datetime64[ns]', length=2250, freq='D')
我正在嘗試根據該變量重新采樣 dataframe。 有什么辦法嗎? 我知道pandas.DataFrame.resample
但它似乎只能用於“常規”重采樣(即每天、每周等)
我是 python 的新手,從 MATLAB 遷移。
謝謝!
這取決於你想怎么做,請查看: http://pandas-docs.github.io/pandas-docs-travis/user_guide/timeseries.ZFC35FDC70D5FC69D2569883A82E
但是,Series 和 DataFrame 也可以直接支持時間分量作為數據本身。
我只舉一些例子:
rng = pd.date_range('1/1/2012', periods=100, freq='S')
ts = pd.Series(np.random.randint(0, 500, len(rng)), index=rng)
ts.resample('W-MON')
idx = pd.date_range('2018-01-01', periods=5, freq='H')
ts = pd.Series(range(len(idx)), index=idx)
Output:
2018-01-01 00:00:00 0
2018-01-01 01:00:00 1
2018-01-01 02:00:00 2
2018-01-01 03:00:00 3
2018-01-01 04:00:00 4
pd.Series(range(3), index=pd.date_range('2000', freq='D', periods=3))
Output:
2000-01-01 0
2000-01-02 1
2000-01-03 2
pd.Series(pd.period_range('1/1/2011', freq='M', periods=3))
Output:
0 2000-01-01
1 2000-01-02
2 2000-01-03
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.