Stéphane
Stéphane

Reputation: 1334

plotting very long scientific numbers in python with pandas?

I need to manipulate very long scientific numbers in python and the pandas dataframe format seems convenient... except I can't plot. For instance, the following baby code:

import pandas as pd
import matplotlib.pyplot as plt
import decimal as dec
obs={}
dec.getcontext().prec=10
obs['x']=[dec.Decimal(1),dec.Decimal(2),dec.Decimal(3)]
obs['y']=[dec.Decimal(1),dec.Decimal(2),dec.Decimal(3)]
df=pd.DataFrame(data=obs)
df.plot(x='x',y='y',marker='*')
plt.show()

gives: TypeError: no numeric data to plot. How should I get my plot ?

Upvotes: 0

Views: 76

Answers (1)

Indiano
Indiano

Reputation: 712

decimal.decimal is an object not a numeric value, you have to do the conversion before plotting!

import pandas as pd
import matplotlib.pyplot as plt
import decimal as dec
obs={}
dec.getcontext().prec=10
obs['x']=[dec.Decimal(1),dec.Decimal(2),dec.Decimal(3)]
obs['y']=[dec.Decimal(1),dec.Decimal(2),dec.Decimal(3)]
df=pd.DataFrame(obs)
print(df.dtypes)
df['x'] = df['x'].astype(int)
df['y'] = df['y'].astype(int)
print(df.dtypes)
df.plot(x ='x',y='y',marker='*')
plt.show()

Outpout

x    object
y    object
dtype: object


x    int32
y    int32
dtype: object

Plot

Upvotes: 1

Related Questions