0

I am facing an issue while plotting contour chart in python as it gives an error message "Value error: could not convert string to float: 03Dec2063". I have data in Excel as per below format.

FinalMaturity | Tenor | Breach

04Jan2021 | 12 | 1.56

04Jan2021 | 12 | 1.74

02Dec2021 | 24 | 1.43

02Dec2021 | 24 | 1.26

......... | .. | ....

03Dec2063 | 48 | 1.18

It's very strange that error talks about only last line date. I attach my code here.

Thanks,

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

path = r'C:\desktop\users\abc.xlsx'
data = pd.read_excel(path)
X,Y = np.meshgrid(data['FinalMaturity'],data['Tenor'])
plt.contourf(X,Y,np.average(data['Breach']))
Add
  • 407
  • 1
  • 4
  • 9
  • 1
    A contour plot needs float values for the X coordinate. In your example: you need to convert the dates to numeric values (like number of days since 01Jan2021 for example). – Alvaro Fuentes Dec 27 '19 at 08:00
  • Pandas can convert strings like this to dates, but you need to tell it how. Do a quick search on “pandas convert string to date” – Jody Klymak Dec 27 '19 at 18:40

1 Answers1

0

Thanks for your suggestion @Alvaro Fuentes. I did converted dates in timestamp by following code.

for dat in range(len(data['FinalMaturity'])):
       new_date = dt.datetime.strptime(data['FinalMaturity'].iloc[dat], %d%b%y)
       timestamp = int(dt.datetime.timestamp(new_date))
       breach_date.append(timestamp)
Add
  • 407
  • 1
  • 4
  • 9