python - 使用索引和来自不同时间序列(具有不同索引)的值创建时间序列

标签 python pandas dataframe time-series

我想使用现有 TimeSerie 的索引和另一个具有不同时间索引的 TimeSerie 的值来创建 DataFrame 或 TimeSerie。时间序列看起来像;

 <class 'pandas.core.series.Series'>
DT
2018-01-02    172.3000
2018-01-03    174.5500
2018-01-04    173.4700
2018-01-05    175.3700
2018-01-08    175.6100
2018-01-09    175.0600
2018-01-10    174.3000
2018-01-11    175.4886
2018-01-12    177.3600
2018-01-17    179.2500
2018-01-18    180.1000
...

<class 'pandas.core.series.Series'>
DT
2018-01-02        NaN
2018-01-09    175.610
2018-01-16    177.360
2018-01-23    180.100
...

我想使用第一个 TS 中的索引并用第二个 TS 中的值填充它。如果在第一个 TS 中找不到索引,则应使用该 TS 中的下一个可用日期。喜欢;

<class 'pandas.core.series.Series'>
DT
2018-01-02   NaN
2018-01-03   NaN
2018-01-04   NaN
2018-01-05   NaN
2018-01-08   NaN
2018-01-09   175.610
2018-01-10   NaN
2018-01-11   NaN
2018-01-12   NaN
2018-01-17   177.360
2018-01-18   NaN
...

谢谢

最佳答案

我的想法是使用reindex两次:

second_ts.reindex(first_ts.index, method='ffill').drop_duplicates().reindex(first_ts.index)

关于python - 使用索引和来自不同时间序列(具有不同索引)的值创建时间序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59923152/

相关文章:

python - gtrends 包给出 "not enough search volume error"

python - 将 f-string 与要在 regex 中使用的原始字符串组合会产生 SyntaxError; ValueError 或错误的结果

python - 将数据框列拆分为 Pandas 中的相等窗口

python - 从 pandas DataFrame 中选择行 : only rows with at least three ones need to be selected

python - 带 yield 的递归函数不返回任何东西

python - Pip 没有选择自定义安装 cmdclass

python - pandas findall() 可以返回 str 而不是 list 吗?

python - 为什么我不能将 x 和 y 标签设置为 pd.plot() 的参数,而我可以轻松设置类似的东西,例如标题?

python - 重构使用 pandas 数据帧的 python 函数

python - 使用 Pandas 获取事件列表