问题
I and to create a heatmap that will have year across the x axis and month across the y axis. In the heatmap will be % returns. Here's kinda what I am after.
So I have some data and I turn them into pct_change() series.
import pandas_datareader.data as web
import pandas as pd
from datetime import datetime as dt
import numpy as np
import seaborn as sns
start = dt(year = 2000, month = 1, day = 1)
df = web.DataReader('GDP', 'fred', start = '2000')
df.pct_change()
df.tail()
So here's what we are working with. Important to note that the index is a Datetime object.
GDP
DATE
2016-10-01 18905.545
2017-01-01 19057.705
2017-04-01 19250.009
2017-07-01 19500.602
2017-10-01 19736.491
I want to do something like this, but I dont know how to implement it with the datetime index
gdp = df.pivot(df.index.month, df.index.year, "GDP")
ax = sns.heatmap(gdp)
Which (expectedly) doesn't work...
KeyError: "Int64Index([ 1, 4, 7, 10, 1, 4, 7, 10, 1, 4, 7, 10, 1, 4, 7, 10, 1,\n 4, 7, 10, 1, 4, 7, 10, 1, 4, 7, 10, 1, 4, 7, 10, 1, 4,\n 7, 10, 1, 4, 7, 10, 1, 4, 7, 10, 1, 4, 7, 10, 1, 4, 7,\n 10, 1, 4, 7, 10, 1, 4, 7, 10, 1, 4, 7, 10, 1, 4, 7, 10,\n 1, 4, 7, 10],\n dtype='int64', name='DATE') not in index"
回答1:
It's not working because you are extracting the month and year in place within the pivot
function, and those information is not in the original df
you specified.
You can specify them beforehand:
df["Year"] = df.DATE.apply(lambda x: x.year)
df["Month"] = df.DATE.apply(lambda x: x.strftime("%B"))
df.pivot_table(index="Month",columns="Year",values="GDP", aggfunc="sum").fillna(0)
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
pt = pt.reindex_axis(months)
sns.heatmap(pt, annot=True)
I'm reindexing the rows because when calling pivot_table
, it sorts columns or rows in ascending order, which is not how the month names are usually sorted.
Above gives me:
来源:https://stackoverflow.com/questions/49058347/seaborn-heatmap-with-datetime-axes