nunodsousa
nunodsousa

Reputation: 2785

Pandas datetime day frequency to week frequency

Q1: I have the following pandas dataframe:

enter image description here

with a huge number of rows with a daily frequency (the Data column). I would like to convert the dataframe in a week base, meaning that the frequency is not diary but now is weekly. Also with this the Money and the workers are the "week sum".

Q2: Is it possible to define the starting day (by date) of the week?

Upvotes: 3

Views: 6794

Answers (1)

piRSquared
piRSquared

Reputation: 294228

first make sure your "Date" column is of type datetime.
Consider this example:

tidx = pd.date_range('2012-01-01', periods=1000)
df = pd.DataFrame(dict(
        Money=np.random.rand(len(tidx)) * 1000,
        Workers=np.random.randint(1, 11, len(tidx)),
        Date=tidx
    ))

When we resample we can pass a string that represents the time unit by which we resample. When using W for weeks we can actually pass W-Mon through W-Sun. So if you have a date

date=pd.to_datetime('2012-03-31')

Which was a Saturday, we can produce the correct resample unit string

'W-{:%a}'.format(date)

'W-Sat'

Then we can resample with it

df.resample('W-{:%a}'.format(date), on='Date').sum().reset_index()

enter image description here

The simple answer is to resample without it, which produces a different starting point.

df.resample('W', on='Date').sum().reset_index()

enter image description here

Upvotes: 5

Related Questions