JstSomeGuy
JstSomeGuy

Reputation: 111

Create lists from dictionary by dynamic date range

The initial input is an OrderedDict that looks like this:

    OrderedDict([(datetime.datetime(2019, 4, 30, 0, 0), 0.0947486624999998),    
    (datetime.datetime(2019, 5, 31, 0, 0), 0.08259463125856992), (datetime.datetime(2019, 6, 30,  
    0, 0), 0.003052897793393905), (datetime.datetime(2019, 7, 31, 0, 0), 0.028023122904952125), 
    (datetime.datetime(2019, 8, 31, 0, 0), 0.07684687634449605), (datetime.datetime(2019, 9, 30, 
    0, 0), -0.03725794433925611), (datetime.datetime(2019, 10, 31, 0, 0), 0.03144787467960408), 
    (datetime.datetime(2019, 11, 30, 0, 0), -0.14988101444115354), (datetime.datetime(2019, 12,   
    31, 0, 0), -0.05752055413222357), (datetime.datetime(2020, 1, 31, 0, 0), 
    0.11857140628117113), (datetime.datetime(2020, 2, 29, 0, 0), 0.021006728910266892), 
    (datetime.datetime(2020, 3, 31, 0, 0), -0.14603720278839682), (datetime.datetime(2020, 4, 
    30, 0, 0), -0.026798450818290687), (datetime.datetime(2020, 5, 31, 0, 0), 
    0.22529234127142295), (datetime.datetime(2020, 6, 30, 0, 0), 0.01974629608463685)])

Currently, I'm using the code below to pull out the dates to put into one row and put the values into a different row.

    from prettytable import PrettyTable

    m = output.analyzers.trtnM.get_analysis()
    months = list(m.keys())
    months_date = [d.strftime('%m-%d-%Y') for d in months]
    returnsM = list(m.values())
    returnsM_2 = [str(round(num * 100, 2)) + "%" for num in returnsM]
    ptM = PrettyTable()
    ptM.title = 'Monthly Returns'
    ptM.field_names = months_date
    ptM.add_row(returnsM_2)

    print(ptM)

This looks like:

+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|                                                                                         Monthly Returns                                                                                          |
+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+
| 04-30-2019 | 05-31-2019 | 06-30-2019 | 07-31-2019 | 08-31-2019 | 09-30-2019 | 10-31-2019 | 11-30-2019 | 12-31-2019 | 01-31-2020 | 02-29-2020 | 03-31-2020 | 04-30-2020 | 05-31-2020 | 06-30-2020 |
+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+
|   9.47%    |   8.26%    |   0.31%    |    2.8%    |   7.68%    |   -3.73%   |   3.14%    |  -14.99%   |   -5.75%   |   11.86%   |    2.1%    |   -14.6%   |   -2.68%   |   22.53%   |   1.97%    |
+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+

With different date ranges it's becoming a bit cumbersome as one long row, so I'd like to be able to split it out so that there is a row for each year and a column for each month. I need this to be dynamic based on the initial OrderedDict, but unsure how to best approach it.

Upvotes: 0

Views: 193

Answers (1)

Tim Fuchs
Tim Fuchs

Reputation: 1191

I guess you mean something like that:

from prettytable import PrettyTable
import calendar

m = output.analyzers.trtnM.get_analysis()

returns = dict()
months = set()

for date, value in m.items():
    if date.year not in returns:
        returns[date.year] = {}
    returns[date.year][date.month] = value
    months.add(date.month)

months = list(sorted(months))

ptM = PrettyTable()
ptM.title = 'Monthly Returns'
ptM.field_names = ["Year"] + [calendar.month_name[month] for month in months]

for year in sorted(returns):
    values = returns[year]
    ptM.add_row([str(year)] + [f"{round(values[month]*100, 2)}%" if month in values else ""
                               for month in months])

print(ptM)

This outputs with your data

+------+---------+----------+--------+--------+--------+-------+------+--------+-----------+---------+----------+----------+
| Year | January | February | March  | April  |  May   |  June | July | August | September | October | November | December |
+------+---------+----------+--------+--------+--------+-------+------+--------+-----------+---------+----------+----------+
| 2019 |         |          |        | 9.47%  | 8.26%  | 0.31% | 2.8% | 7.68%  |   -3.73%  |  3.14%  | -14.99%  |  -5.75%  |
| 2020 |  11.86% |   2.1%   | -14.6% | -2.68% | 22.53% | 1.97% |      |        |           |         |          |          |
+------+---------+----------+--------+--------+--------+-------+------+--------+-----------+---------+----------+----------+

Upvotes: 1

Related Questions