quantopian / pyfolio

Portfolio and risk analytics in Python
https://quantopian.github.io/pyfolio
Apache License 2.0
5.71k stars 1.78k forks source link

removing deprecated pd.TimeGrouper and using the .xs multi level indexing versus [] #625

Open derekwong9 opened 4 years ago

derekwong9 commented 4 years ago

the capacity tearsheet had several errors when following the format of market_data as found in the /pyfolio/tests

dates = pd.date_range(start='2015-01-01', freq='D', periods=3)
positions = pd.DataFrame([[1.0, 3.0, 0.0],
                       [0.0, 1.0, 1.0],
                       [3.0, 0.0, 1.0]],
                      columns=['A', 'B', 'cash'], index=dates)

transactions = pd.DataFrame(data=[[1, 100000, 10, 'A']] * len(dates),
                         columns=['sid', 'amount', 'price', 'symbol'],
                         index=dates)

volume = pd.DataFrame([[1.0, 3.0],
                    [2.0, 2.0],
                    [3.0, 1.0]],
                   columns=['A', 'B'], index=dates)
volume.index.name = 'dt'
volume = volume * 1000000
volume['market_data'] = 'volume'
price = pd.DataFrame([[1.0, 1.0]] * len(dates),
                  columns=['A', 'B'], index=dates)
price.index.name = 'dt'
price['market_data'] = 'price'
market_data = pd.concat([volume, price]).reset_index().set_index(
    ['dt', 'market_data'])

using this example you have a 2 level index whereby you are then calling market data, originally the indexer was ['price'] which was causing errors and non-explicit. using the .xs makes it much clearer and also functional.

pd.TimeGrouper has been deprecated and has been replaced by pd.Grouper, the parameter remains the same of 'D' for pd.Grouper(freq='D')