I'm trying to calculate the inventory of stocks from a table in monthly buckets in Pandas. This is the table:
Goods | Incoming | Date
-------+------------+-----------
'a' | 10 | 2014-01-10
'a' | 20 | 2014-02-01
'b' | 30 | 2014-01-02
'b' | 40 | 2014-05-13
'a' | 20 | 2014-06-30
'c' | 10 | 2014-02-10
'c' | 50 | 2014-05-10
'b' | 70 | 2014-03-10
'a' | 10 | 2014-02-10
This is my code so far:
import pandas as pd
df = pd.DataFrame({
'goods': ['a', 'a', 'b', 'b', 'a', 'c', 'c', 'b', 'a'],
'incoming': [0, 20, 30, 40, 20, 10, 50, 70, 10],
'date': ['2014-01-10', '2014-02-01', '2014-01-02', '2014-05-13', '2014-06-30', '2014-02-10', '2014-05-10', '2014-03-10', '2014-02-10']})
df['date'] = pd.to_datetime(df['date'])
# we don't care about year in this example
df['month'] = df['date'].map(lambda x: x.month)
dfg = df.groupby(['goods', 'month'])['incoming'].sum()
# flatten multi-index
dfg = dfg.reset_index ()
dfg['level'] = dfg.groupby(['goods'])['incoming'].cumsum()
dfg
which returns
goods month incoming level
0 a 1 0 0
1 a 2 30 30
2 a 6 20 50
3 b 1 30 30
4 b 3 70 100
5 b 5 40 140
6 c 2 10 10
7 c 5 50 60
While this is good, the visualisation method that I use requires (1) the same number of data points per group ('goods'), (2) the same extent of the time-series (i.e. earliest/latest month is the same for all time series) and (3) that there are no "gaps" in any time series (a month between min(month) and max(month) with a data point).
How can I do this with Pandas? Note, even thought this structure may be a bit inefficient, I'd like to stick with the general flow of things. Perhaps it's possible to insert some "post-processing" to fill in the gaps.
Update
To summarise the response below, I chose to do this:
piv = dfg.pivot_table(["level"], "month", "goods")
piv = piv.reindex(np.arange(piv.index[0], piv.index[-1] + 1))
piv = piv.ffill(axis=0)
piv = piv.fillna(0)
piv.index.name = 'month'
I also added
piv = piv.stack()
print r.reset_index()
to get a table similar to the input table:
month goods level
0 1 a 0
1 1 b 30
2 1 c 0
3 2 a 30
4 2 b 30
5 2 c 10
6 3 a 30
7 3 b 100
8 3 c 10
9 4 a 30
10 4 b 100
11 4 c 10
12 5 a 30
13 5 b 140
14 5 c 60
15 6 a 50
16 6 b 140
17 6 c 60
I think you want to use
pivot_table
:To get the filled in months, you can reindex (this feels a little hacky, there may be a neater way):
One issue here is that month is independent of year, in may be preferable to have a period index:
and then you can reindex by the period range:
Which is to say, you can do the same with your
dfg
:and reindex.