简体   繁体   English

如何将时间序列 dataframe 按月份分组,plot x 轴作为月份分组?

[英]How to group a time series dataframe by day of the month and plot the x axis as the day of the month?

I'm analyzing NOAA Global Historical Climatology Network Daily that is stored in BigQuery.我正在分析存储在 BigQuery 中的 NOAA 全球历史气候网络日报。 I want to understand if max temperatures (on the same day of the year) have changed from year to year to understand climate change (ie 'can we see a subtle rise in temps from August 25th 1970 vs. August 25th 1980' and so on).我想了解最高温度(一年中的同一天)是否逐年变化以了解气候变化(即“我们能否看到从 1970 年 8 月 25 日到 1980 年 8 月 25 日的温度略有上升”等等)。

I'm able to get the data pulled fine using the BigQuery Colab Client.我可以使用 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)

    

This creates a dataframe that looks like so:这将创建一个 dataframe,如下所示:

数据框

I tried plotting it like so我试着像这样绘制它

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

图表

This is showing it on a year by year basis, even though I'm only focused on a specific stretch of 15-20 days.尽管我只关注 15-20 天的特定时间段,但它每年都在显示它。 I'd like to be able to show only those specific days.我希望能够只显示那些特定的日子。 So something like the 1st day of September (and then have all of the bars for that day across many years) and then the 2nd, etc. etc.所以就像九月的第一天(然后有很多年那天的所有酒吧),然后是第二天,等等。

How do I group on a day of the year or a specific month?如何在一年中的某一天或特定月份进行分组?

If need filter between 1.9.如果需要在 1.9 之间进行过滤。 to 20.9 for all years use:到 20.9 为所有年份使用:

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

s.plot()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM