python - Pandas Resample 上采样数据的最后日期/边缘

标签 python python-3.x pandas datetime reindex

我正在尝试将每周数据上采样到每日数据,但是,我在上采样最后一个边缘时遇到了困难。我该怎么做?

import pandas as pd
import datetime
df = pd.DataFrame({'wk start': ['2018-08-12', '2018-08-12', '2018-08-19'], 
    'car': [ 'tesla model 3', 'tesla model x', 'tesla model 3'],
    'sales':[38000,98000, 40000]})
df['wk start'] = df['wk start'].apply(lambda x: datetime.datetime.strptime(x, '%Y-%m-%d'))
df.set_index('wk start').groupby('car').resample('D').pad()

返回:

                             car            sales
car             wk start        
tesla model 3   2018-08-12  tesla model 3   38000
                2018-08-13  tesla model 3   38000
                2018-08-14  tesla model 3   38000
                2018-08-15  tesla model 3   38000
                2018-08-16  tesla model 3   38000
                2018-08-17  tesla model 3   38000
                2018-08-18  tesla model 3   38000
                2018-08-19  tesla model 3   40000

tesla model x   2018-08-12  tesla model x   98000

我想要的输出是:

                             car            sales
car             wk start        
tesla model 3   2018-08-12  tesla model 3   38000
                2018-08-13  tesla model 3   38000
                2018-08-14  tesla model 3   38000
                2018-08-15  tesla model 3   38000
                2018-08-16  tesla model 3   38000
                2018-08-17  tesla model 3   38000
                2018-08-18  tesla model 3   38000
                2018-08-19  tesla model 3   40000
                2018-08-20  tesla model 3   40000
                2018-08-21  tesla model 3   40000
                2018-08-22  tesla model 3   40000
                2018-08-23  tesla model 3   40000
                2018-08-24  tesla model 3   40000
                2018-08-25  tesla model 3   40000
tesla model x   2018-08-12  tesla model x   98000
                2018-08-13  tesla model x   98000
                2018-08-14  tesla model x   98000
                2018-08-15  tesla model x   98000
                2018-08-16  tesla model x   98000
                2018-08-17  tesla model x   98000
                2018-08-18  tesla model x   98000

我看了this ,但他们正在使用句点,而我正在查看日期时间。提前致谢!

最佳答案

是的,你是对的,最后的边缘数据被排除在外。解决方案是将它们添加到输入 DataFrame - 我的解决方案使用 drop_duplicates 创建了一个助手 Dataframe , 添加 6 天和 concat在使用您的解决方案之前将其更改为原始 df:

df1 = df.sort_values('wk start').drop_duplicates('car', keep='last').copy()
df1['wk start'] = df1['wk start'] + pd.Timedelta(6, unit='d')

df = pd.concat([df, df1], ignore_index=True)
df = df.set_index('wk start').groupby('car').resample('D').pad()
print (df)
                                    car  sales
car           wk start                        
tesla model 3 2018-08-12  tesla model 3  38000
              2018-08-13  tesla model 3  38000
              2018-08-14  tesla model 3  38000
              2018-08-15  tesla model 3  38000
              2018-08-16  tesla model 3  38000
              2018-08-17  tesla model 3  38000
              2018-08-18  tesla model 3  38000
              2018-08-19  tesla model 3  40000
              2018-08-20  tesla model 3  40000
              2018-08-21  tesla model 3  40000
              2018-08-22  tesla model 3  40000
              2018-08-23  tesla model 3  40000
              2018-08-24  tesla model 3  40000
              2018-08-25  tesla model 3  40000
tesla model x 2018-08-12  tesla model x  98000
              2018-08-13  tesla model x  98000
              2018-08-14  tesla model x  98000
              2018-08-15  tesla model x  98000
              2018-08-16  tesla model x  98000
              2018-08-17  tesla model x  98000
              2018-08-18  tesla model x  98000

关于python - Pandas Resample 上采样数据的最后日期/边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51790793/

相关文章:

python - 如何在 Python 中将 OpenCV 导入 NAO?

python - 什么数据结构容器可以按日期排序

javascript - 如何按 dict 值分组并存储到 JavaScript 列表中

Python在同一图表上绘制条形图和百分比折线图

python - Django 对与外键对象相关的组进行排序

python - Django 模型继承 - 只需要查询中的父类实例

python - Django ORM 按 ID 分组

python - 我怎样才能调出一个带有已经用 python 脚本导入的包的 python shell?

python - 重新索引(重新映射)列的值

python - 将参数从列表传递给 python plumbum 命令