简体   繁体   English

Pandas:绘图时忽略索引中的年份,但保留小时/天/月

[英]Pandas: ignore year in index when plotting but keep hour/day/month

I have the following data series:我有以下数据系列:

                     values
Date                              
2013-01-01 00:00:00            NaN
2013-01-01 01:00:00       0.041702
2013-01-01 02:00:00       0.042505
2013-01-01 03:00:00       0.030535
...
2020-12-30 21:00:00       0.525059
2020-12-30 22:00:00       0.249274
2020-12-30 23:00:00       0.024965     

I want to:我想要:

  1. roll all years,滚动所有年份,
  2. align them by day of the week,按星期几对齐,
  3. calculate statistics such as the mean for that day across the eight years of data---Yes, eight points is too few for statistics.计算八年数据中当天的平均值等统计数据---是的,八个点对于统计数据来说太少了。 Eg, point 0 would be the mean of the values of the first Tuesdays of each year at 00:00:00, or 2013-01-01 00:00:00 , 2014-01-07 00:00:00 , 2015-01-06 00:00:00 , etc.例如,点 0 将是每年第一个星期二 00:00:00 或2013-01-01 00:00:002014-01-07 00:00:002015-01-06 00:00:00的平均值2015-01-06 00:00:00

Plotting this would basically result in a plot with a single line based on about 365 point estimates (ignoring leap years and days with fewer data at the end of the year).绘制此图基本上会导致 plot 具有基于大约 365 点估计的单线(忽略闰年和年末数据较少的天数)。 I tried starting with pivot tables as suggested here but failed miserably:我尝试按照此处的建议从 pivot 表开始,但失败了:

df_pv = pd.pivot_table(series.to_frame(), columns=series.index.year)

Exception has occurred: AttributeError 'Series' object has no attribute 'columns'

Any ideas?有任何想法吗?

It's exactly as you have written down, just a groupby() not a pivot.就像你写的那样,只是一个groupby()而不是 pivot。

import datetime as dt
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=[10,6])

d = pd.date_range(dt.date(2013,1,1), dt.date(2021,1,1), freq="H")
df = pd.DataFrame({"Date":d,"values":np.random.uniform(0,1,len(d))})


l = df.groupby([df.Date.dt.dayofweek,df.Date.dt.isocalendar().week,df.Date.dt.time]).agg({"values":"mean"}).plot(ax=ax)
l = ax.set_xticklabels(ax.get_xticklabels(), rotation = 90)

在此处输入图像描述

暂无
暂无

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

相关问题 如何指定在大熊猫(小时,分钟,秒,日,月,年)中读取时间值时首先出现的内容? - How to specify what comes first when reading time values in pandas (hour, minute, seconds, day, month, year)? Python:按小时,日和月(按年份分组)过滤熊猫中的DataFrame - Python: Filter DataFrame in Pandas by hour, day and month grouped by year 熊猫:当索引为年月且列为日时,将DataFrame转换为Series - Pandas: Convert a DataFrame into a Series when index is Year-Month and columns are Day 熊猫时间序列重采样:KeyError:“ [['year''month''day']不在索引中” - Pandas Time Series Resampling: KeyError: “['year' 'month' 'day'] not in index” 按年将 DataFrame 组织成列并按日月索引 - PYTHON - PANDAS - Organize DataFrame into columns by year and index by day-month - PYTHON - PANDAS 如何为 Pandas 中的 2 天数据从年、日、小时和分钟列(无月列)创建日期时间 object? - How to create a datetime object from Year, Day, Hour and Minute columns (without month column) for 2 day data in Pandas? python中的时间戳(年、月、日、小时、分钟) - Timestamp in python (year,month,day,hour,minute) 截断SQLAlchemy中的小时/天/周/月/年 - Truncate Hour/Day/Week/Month/Year in SQLAlchemy 按分钟,小时,天,月和年分组? - groupby minute, hour, day, month, and year? 将时间戳转换为日,月,年和小时 - Convert timestamp to day, month, year and hour
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM