Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

python - pandas Dataframe resampling with specific dates

I have a question regarding the resampling method of pandas Dataframes. I have a DataFrame with one observation per day:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(0,100,size=(366, 1)), columns=list('A'))
df.index = pd.date_range(datetime.date(2016,1,1),datetime.date(2016,12,31))

if I want to compute the sum (or other) for every month, I can directly do:

EOM_sum = df.resample(rule="M").sum()

however I have a specific calendar (irregular frequency):

import datetime
custom_dates = pd.DatetimeIndex([datetime.date(2016,1,13),
                             datetime.date(2016,2,8),
                             datetime.date(2016,3,16),
                             datetime.date(2016,4,10),
                             datetime.date(2016,5,13),
                             datetime.date(2016,6,17),
                             datetime.date(2016,7,12),
                             datetime.date(2016,8,11),
                             datetime.date(2016,9,10),
                             datetime.date(2016,10,9),
                             datetime.date(2016,11,14),
                             datetime.date(2016,12,19),
                             datetime.date(2016,12,31)])

If I want to compute the sum for each period, I currently add a temporary column to df with the end of the period each row belongs to, and then perform the operation with a groupby:

df["period"] = custom_dates[custom_dates.searchsorted(df.index)]
custom_sum = df.groupby(by=['period']).sum()

However this is quite dirty and doesn't look pythonic. Is there a built-in method to do this in Pandas? Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Creating nw column is not necessary, you can groupby by DatatimeIndex, because length is same as lenght of df:

import pandas as pd
import numpy as np

np.random.seed(100)
df = pd.DataFrame(np.random.randint(0,100,size=(366, 1)), columns=list('A'))
df.index = pd.date_range(datetime.date(2016,1,1),datetime.date(2016,12,31))
print (df.head())
             A
2016-01-01   8
2016-01-02  24
2016-01-03  67
2016-01-04  87
2016-01-05  79

import datetime
custom_dates = pd.DatetimeIndex([datetime.date(2016,1,13),
                             datetime.date(2016,2,8),
                             datetime.date(2016,3,16),
                             datetime.date(2016,4,10),
                             datetime.date(2016,5,13),
                             datetime.date(2016,6,17),
                             datetime.date(2016,7,12),
                             datetime.date(2016,8,11),
                             datetime.date(2016,9,10),
                             datetime.date(2016,10,9),
                             datetime.date(2016,11,14),
                             datetime.date(2016,12,19),
                             datetime.date(2016,12,31)])
custom_sum = df.groupby(custom_dates[custom_dates.searchsorted(df.index)]).sum()
print (custom_sum)
               A
2016-01-13   784
2016-02-08  1020
2016-03-16  1893
2016-04-10  1242
2016-05-13  1491
2016-06-17  1851
2016-07-12  1319
2016-08-11  1348
2016-09-10  1616
2016-10-09  1523
2016-11-14  1793
2016-12-19  1547
2016-12-31   664

Another solution is append new index by custom_dates, groupby use numpy array as output from searchsorted function:

print (custom_dates.searchsorted(df.index))
[ 0  0  0  0  0  0  0  0  0  0  0  0  0  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  2  2  2  2  2  2  2  2  2  2  2
  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2
  2  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3
  3  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4
  4  4  4  4  4  4  4  4  4  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5
  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  6  6  6  6  6  6
  6  6  6  6  6  6  6  6  6  6  6  6  6  6  6  6  6  6  6  7  7  7  7  7  7
  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  8
  8  8  8  8  8  8  8  8  8  8  8  8  8  8  8  8  8  8  8  8  8  8  8  8  8
  8  8  8  8  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9
  9  9  9  9  9  9  9  9 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 11 11 11 11 11 11
 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11
 11 11 11 11 12 12 12 12 12 12 12 12 12 12 12 12]

custom_sum = df.groupby(custom_dates.searchsorted(df.index)).sum()
custom_sum.index = custom_dates
print (custom_sum)
               A
2016-01-13   784
2016-02-08  1020
2016-03-16  1893
2016-04-10  1242
2016-05-13  1491
2016-06-17  1851
2016-07-12  1319
2016-08-11  1348
2016-09-10  1616
2016-10-09  1523
2016-11-14  1793
2016-12-19  1547
2016-12-31   664

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...