-1

The code is copied from this main link.

Here's the code, that showed me the error:

import pandas as pd
import seaborn as sns
year = 2022
url = f"https://home.treasury.gov/resource-center/data-chart-center/interest-rates/TextView?type=daily_treasury_yield_curve&field_tdr_date_value={year}"
table_list = pd.read_html(url)
df = table_list[0]
def clean_df(df):  
  df['Date'] = pd.to_datetime(df['Date'])# .apply(pd.Timestamp.toordinal)
  df = df.drop(['20 YR', '30 YR', 'Extrapolation Factor',
        '8 WEEKS BANK DISCOUNT', 'COUPON EQUIVALENT', '52 WEEKS BANK DISCOUNT',
        'COUPON EQUIVALENT.1'], axis=1)
  return df
df = clean_df(df)
sns.heatmap(df)

this is showing the error:

TypeError                                 Traceback (most recent call last)
<ipython-input-237-10d89d2ec370> in <module>()
----> 1 sns.heatmap(df);

3 frames
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py in inner_f(*args, **kwargs)
     44             )
     45         kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 46         return f(**kwargs)
     47     return inner_f
     48 

/usr/local/lib/python3.7/dist-packages/seaborn/matrix.py in heatmap(data, vmin, vmax, cmap, center, robust, annot, fmt, annot_kws, linewidths, linecolor, cbar, cbar_kws, cbar_ax, square, xticklabels, yticklabels, mask, ax, **kwargs)
    540     plotter = _HeatMapper(data, vmin, vmax, cmap, center, robust, annot, fmt,
    541                           annot_kws, cbar, cbar_kws, xticklabels,
--> 542                           yticklabels, mask)
    543 
    544     # Add the pcolormesh kwargs here

/usr/local/lib/python3.7/dist-packages/seaborn/matrix.py in __init__(self, data, vmin, vmax, cmap, center, robust, annot, fmt, annot_kws, cbar, cbar_kws, xticklabels, yticklabels, mask)
    158         # Determine good default values for the colormapping
    159         self._determine_cmap_params(plot_data, vmin, vmax,
--> 160                                     cmap, center, robust)
    161 
    162         # Sort out the annotations

/usr/local/lib/python3.7/dist-packages/seaborn/matrix.py in _determine_cmap_params(self, plot_data, vmin, vmax, cmap, center, robust)
    191 
    192         # plot_data is a np.ma.array instance
--> 193         calc_data = plot_data.astype(float).filled(np.nan)
    194         if vmin is None:
    195             if robust:

TypeError: float() argument must be a string or a number, not 'Timestamp'

to check for solution on stackoverflow I got these: link1 and link2; Which solves the error, but while plotting on seaborn, all of the data are shown in black in the image.

I have used the solutions on this link also and I am still facing the same issue.

Further using plotpy as in the main link also giving me a same kind of result. How to avoid this issue?

I have no permission to show image here.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
NightSky
  • 1
  • 3
  • You forgot this `df = df.set_index('Date')` line in `def clean_df` from `In [20]:` in the [tutorial](https://pieriantraining.com/visualizing-historical-yield-curves-with-plotly-and-python/?utm_source=udemy&utm_medium=referral&utm_campaign=site_live_announcement&utm_source=email-sendgrid&utm_medium=903744&utm_campaign=2022-07-22&utm_term=9685726&utm_content=educational). I'm voting to close this as not reproducible / caused by a typo. – Trenton McKinney Jul 25 '22 at 16:56

1 Answers1

0

You need to set the index to get meaningful y-axis labels. In the given case the Date column has entries for each day, so you can directly use it without converting to datetime.

df = table_list[0]
df.index = df['Date']
df = df[['1 Mo', '2 Mo', '3 Mo', '6 Mo', '1 Yr', '2 Yr', '3 Yr', '5 Yr', '7 Yr', '10 Yr', '20 Yr', '30 Yr']]
ax = sns.heatmap(df)

enter image description here

Stef
  • 28,728
  • 2
  • 24
  • 52