Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from io import StringIO
- import pandas as pd
- import numpy as np
- text = """time price quantity initiator
- 2016-07-13 16:19:31 6.20 8000 B
- 2016-07-13 16:19:45 6.19 5176 S
- 2016-07-13 16:25:08 6.24 15000 NaN
- 2016-07-13 16:25:08 6.24 2847 S
- 2016-07-13 16:25:08 6.24 39829 B
- 2016-07-13 16:25:08 6.24 2398 B
- 2016-07-13 16:25:08 6.24 1844 NaN
- 2016-07-13 16:25:08 6.24 9538 S
- 2016-07-13 16:25:08 6.24 459 B
- 2016-07-13 16:25:08 6.24 1082 B
- 2016-07-14 16:19:31 6.20 8000 B
- 2016-07-14 16:19:45 6.19 5176 S
- 2016-07-14 16:25:08 6.24 15000 NaN
- 2016-07-14 16:25:08 6.24 2847 S
- 2016-07-14 16:25:08 6.24 39829 B
- 2016-07-14 16:25:08 6.24 2398 B
- 2016-07-14 16:25:08 6.24 1844 NaN
- 2016-07-14 16:25:08 6.24 9538 S
- 2016-07-14 16:25:08 6.24 459 B
- 2016-07-14 16:25:08 6.24 1082 B"""
- df = pd.read_csv(StringIO(text), sep='\s{2,}', engine='python', index_col=0, parse_dates=[0])
- # B: add; S: subtract; NaN: do nothing
- """
- Use np.where() and notnull to create series of [-1, 0, 1]s.
- Multiply this series by df.quantity and use cumsum()
- """
- def accumulator(df):
- initiator = np.where(df.initiator == 'B', 1, -1) * df.initiator.notnull()
- return pd.DataFrame((df.quantity * initiator).cumsum(), df.index, ['acc_quantity'])
- pd.concat([df, df.groupby(df.index.strftime('%Y-%m-%d')).apply(accumulator)], axis=1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement