DataFrame Pandas shows NAN

I have hdf5 and I have moved to DataFrame, but problem is when I want to plot, nothing shows on the graph. And I have checked new dataframe, but I saw, there was nothing. This is my DF ( I don't allowed to post pics, so please click to the link )

df1 = pd.DataFrame(df.Price, index = df.Timestamp)
plt.figure()
df1.plot()
plt.show()

Second DF shows NAN in price column. Whats wrong?

Upvotes: 1

Views: 6499

Answers (2)

Anton vBR
Anton vBR

Reputation: 18916

You can do this by adding .values, And how about creating a series instead?

#df1 = pd.DataFrame(df.Price.values, df.Timestamp)
serie = pd.Series(df.Price.values, df.Timestamp)

Saw it was answered here: pandas.Series() Creation using DataFrame Columns returns NaN Data entries

Full example:

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

df = pd.DataFrame(columns=["Price","Timestamp","Random"])
df.Price = np.random.randint(100, size = 10)
df.Timestamp = [datetime.datetime(2000,1,1) + \
            datetime.timedelta(days=int(i)) for i in np.random.randint(100, size = 10)]
df.Random = np.random.randint(10, size= 10)

serie = pd.Series(df.Price.values, df.Timestamp)

serie.plot()
plt.show()

enter image description here


Difference

print("{}\n{}".format(type(df.Price), type(df.Price.values)))
<class 'pandas.core.series.Series'> # does not work
<class 'numpy.ndarray'> # works

Upvotes: 2

jezrael
jezrael

Reputation: 862641

I think you need set_index from column Timestamp, select column Price and plot:

#convert column to floats
df['Price'] = df['Price'].astype(float)
df.set_index('Timestamp')['Price'].plot()

#if some non numeric data, convert them to NaNs
df['Price'] = pd.to_numeric(df['Price'], errors='coerce')
df.set_index('Timestamp')['Price'].plot()

And get NaNs if use DataFrame constructor, because data not aligned - values of index of df are not same as Timestamp column.

Upvotes: 2

Related Questions