Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate daily sums using python pandas

Tags:

python

pandas

I'm trying to calculate daily sums of values using pandas. Here's the test file - http://pastebin.com/uSDfVkTS

This is the code I came up so far:

import numpy as np
import datetime as dt
import pandas as pd

f = np.genfromtxt('test', dtype=[('datetime', '|S16'), ('data', '<i4')], delimiter=',')
dates = [dt.datetime.strptime(i, '%Y-%m-%d %H:%M') for i in f['datetime']]
s = pd.Series(f['data'], index = dates)
d = s.resample('D', how='sum')

Using the given test file this produces:

2012-01-02    1128
Freq: D

First problem is that calculated sum corresponds to the next day. I've been able to solve that by using parameter loffset='-1d'.

Now the actual problem is that the data may start not from 00:30 of a day but at any time of a day. Also the data has gaps filled with 'nan' values.

That said, is it possible to set a lower threshold of number of values that are necessary to calculate daily sums? (e.g. if there're less than 40 values in a single day, then put NaN instead of a sum)

I believe that it is possible to define a custom function to do that and refer to it in 'how' parameter, but I have no clue how to code the function itself.

like image 937
iodinegalaxy Avatar asked Nov 20 '12 14:11

iodinegalaxy


1 Answers

You can do it directly in Pandas:

s = pd.read_csv('test', header=None, index_col=0, parse_dates=True)
d = s.groupby(lambda x: x.date()).aggregate(lambda x: sum(x) if len(x) >= 40 else np.nan)

             X.2
2012-01-01  1128
like image 123
eumiro Avatar answered Oct 10 '22 06:10

eumiro