0

I have the following DataFrame:

df = pd.DataFrame({
'Trader': 'Carl Mark Carl Joe Mark Carl Max Max'.split(),
'Share': list('ABAABAAA'),
'Quantity': [5,2,5,10,1,5,2,1]
}, index=[
    DT.datetime(2013,1,1,13,0),
    DT.datetime(2013,1,1,13,5),
    DT.datetime(2013,1,1,20,0),
    DT.datetime(2013,1,2,10,0),
    DT.datetime(2013,1,2,12,0),                                      
    DT.datetime(2013,1,2,14,0),
    DT.datetime(2013,6,2,14,0),
    DT.datetime(2013,7,2,14,0),
    ])

Is it possible to create a Period object on a daily basis which abstracts from the concrete day. I would like to evaluate the question whether there is a tendency among the traders in the sample to trade lower volumes.

To do so I would like to create a table like this:

Period | Trader | Quantity
--------------------------
1      | Carl   | 10
1      | Mark   | 2
1      | Joe    | 10
1      | Max    | 2
2      | Carl   | 5
2      | Mark   | 1
2      | Max    | 1

Andy

Update:

The Datasampel above was too simple to show my problem. I hope to create a period object which abstracts from the concrete date. My goal is to compare the sequence of the occurred trades per trader.

df1 = pd.DataFrame({
'Trader': 'Carl Mark Carl Joe Mark Carl Max Max'.split(),
'Share': list('ABAABAAA'),
'Quantity': [5,2,5,10,1,5,2,1]
}, index=[
    DT.datetime(2013,1,1,13,0),
    DT.datetime(2013,1,1,13,5),
    DT.datetime(2013,1,1,20,0),
    DT.datetime(2013,2,6,10,0),
    DT.datetime(2013,2,5,12,0),                                      
    DT.datetime(2013,3,7,14,0),
    DT.datetime(2013,6,4,14,0),
    DT.datetime(2013,7,4,14,0),
    ])
Andy
  • 9,483
  • 12
  • 38
  • 39

1 Answers1

0

This will get you your table

In [22]: x = df.reset_index()

In [23]: x['day'] = x['index'].apply(lambda x: x.day)

In [24]: x
Out[24]: 
                index  Quantity Share Trader  day
0 2013-01-01 13:00:00         5     A   Carl    1
1 2013-01-01 13:05:00         2     B   Mark    1
2 2013-01-01 20:00:00         5     A   Carl    1
3 2013-01-02 10:00:00        10     A    Joe    2
4 2013-01-02 12:00:00         1     B   Mark    2
5 2013-01-02 14:00:00         5     A   Carl    2
6 2013-06-02 14:00:00         2     A    Max    2
7 2013-07-02 14:00:00         1     A    Max    2

But this is probably what you want

In [25]: x.groupby(['day','Trader']).sum()
Out[25]: 
            Quantity
day Trader          
1   Carl          10
    Mark           2
2   Carl           5
    Joe           10
    Mark           1
    Max            3
Jeff
  • 125,376
  • 21
  • 220
  • 187
  • Hi Jeff. Thanks for your help, however, I am afraid my example was misleading. It is not enough to extract only the day. Instead I am looking to extract the sequence of the trades. I updated my original post with a more complex example. – Andy May 19 '13 at 10:40