python - Pandas系列在Python中按月份索引排序(时间序列不同)

标签 python pandas datetime

我有一个 Series 对象,其中包含:

df = 
    index              value
2014-05-23 07:00:00     0.67
2014-05-23 07:30:00     0.47
2014-05-23 08:00:00     0.42
2014-05-23 08:30:00     0.80
....

2017-07-10 22:00:00     0.42
2017-07-10 22:30:00     0.79
2017-07-10 23:00:00     0.84
2017-07-10 23:30:00     Nan

我想计算一年的平均值,然后按月分组,所以数据框看起来像这样,

df_new = 
  index                    value
   Jan      {0.11, 0.5, 0.3, 0.99, ... ,0.13} <-  time step of each value is 
   Feb      {...............................}     still 30 min, and each 
   Mar      {...............................}     value is average of same 
   Apr      {...............................}     time in the other year.  
   ....
   Dec      {...............................}

我有一些像这样的数据帧,但具有不同的时间间隔(15分钟,60分钟...),有没有更好的方法自动计算它?例如像函数一样,它会自动从索引中知道时间步长。提前致谢!

最佳答案

我认为首先需要通过 resample 进行上采样或下采样:

#upsample
s = s.resample('15Min').ffill()
#downsample
#s = s.resample('60Min').mean()
#if already 30 minutes values no resample necessary

然后groupby 秒按 DatetimeIndex.strftime转换为ordered CategoricalDatetimeIndex.time ,聚合均值和最后一次 reshape unstack :

cats = ['Jan', 'Feb', 'Mar', 'Apr','May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
months = pd.Categorical(s.index.strftime('%b'), categories=cats, ordered=True)
df = s.groupby([months, s.index.time]).mean().unstack()

关于python - Pandas系列在Python中按月份索引排序(时间序列不同),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49601166/

相关文章:

python - Google 群组 CAN_REQUEST_TO_JOIN

python - 使用 Python 和 Numpy 将 2 个图像混合为 1 个

Python:适用于 Linux 和 Windows 的 OAuth 库

python - 如何简化代码以检查 pandas 中的范围

Python pandas 替换函数不适用于转义字符

python - 删除包含 NaN、NaTs 和 nans 的任何列的行

c# - 用于 Web 应用程序的 C# 中的日期时间

python - 根据数据框中的其他值更改 pandas 数据框的值

c# - C# 中的一天中的时间结构

java - 获取最近 12 小时的条目 SQLite