【问题标题】:How to group a time series dataframe by day of the month and plot the x axis as the day of the month?如何按月份的日期对时间序列数据框进行分组并将 x 轴绘制为月份的日期?
【发布时间】:2022-09-23 14:12:46
【问题描述】:

我正在分析存储在 BigQuery 中的 NOAA 全球历史气候网络日报。我想了解最高温度(一年中的同一天)是否每年都在变化以了解气候变化(即“我们能否看到 1970 年 8 月 25 日与 1980 年 8 月 25 日之间的温度略有上升”和很快)。

我可以使用 BigQuery Colab 客户端获取数据。

dfall = pd.DataFrame()

for i in range(1991,2010):
    sql = \"SELECT date, element, (value/10 * 1.8) + 32 as temp_f, extract(year from date) yearstring  FROM `bigquery-public-data.ghcn_d.ghcnd_\" + str(i) + \"` where id = \'USC00040693\' and DATE(date) bETWEEN DATE(\'\" + str(i) + \"-08-26\') AND DATE(\'\"+ str(i) + \"-09-03\') and (element = \'TMAX\') order by date asc \"
     
    dfyear = client.query(sql).to_dataframe()
    dfall = dfall.append(dfyear, ignore_index=True)

    

这将创建一个如下所示的数据框:

我试着像这样绘制它

dfall.set_index(\'date\').plot()

尽管我只专注于 15-20 天的特定时间段,但它每年都会显示出来。我希望能够只显示那些特定的日子。所以就像九月的第一天(然后有很多年那天的所有酒吧),然后是第二天,等等。

如何在一年中的某一天或特定月份进行分组?

    标签: python pandas google-bigquery time-series timeserieschart


    【解决方案1】:

    如果需要在 1.9 之间进行过滤。到 20.9 为所有年份使用:

    dfall['date'] = pd.to_datetime(dfall['date'])
    
    s = dfall.set_index('date')['temp_f']
    s = s[(s.dt.month == 9) & s.dt.day.between(1,20)]
    
    s.plot()
    

    【讨论】:

    • AttributeError: Can only use .dt accessor with datetimelike values越来越暖和了!
    • @KylePennell - 添加dfall['date'] = pd.to_datetime(dfall['date']),答案已编辑。
    猜你喜欢
    • 1970-01-01
    • 2017-10-17
    • 2018-02-11
    • 2019-02-02
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 2018-10-16
    • 2018-10-03
    相关资源
    最近更新 更多