Daniele Piccolo
Daniele Piccolo

Reputation: 155

pd.series changes the value of a column

I'm trying to create a Calendar heatmaps from a csv that contains rainy values (VM) and dates (ISTANTE).

IDSTAZ,SENSORE,ISTANTE,VM
56,300001896,201601010000,0.0
56,300001896,201601020000,0.2
56,300001896,201601030000,0.6
56,300001896,201601040000,1.8

Searching on the web I have write the code below:

import pandas as pd
import calmap
import matplotlib.pyplot as plt

df = pd.read_csv('D:\\GIS\\Dati\\Meteo_arpav\\stazione_forno.csv')
df['DateTime'] = df['ISTANTE'].apply(lambda x: pd.to_datetime(str(x), format='%Y%m%d%H%M'))
events = pd.Series((df['VM']), index=df['DateTime'])
a=calmap.calendarplot(events)
plt.show(a)

It worked but when I create the seriesall the values of VM became NaN. Printing (events) this is the result:

DateTime
2016-01-01   NaN
2016-01-02   NaN
2016-01-03   NaN
2016-01-04   NaN
2016-01-05   NaN
2016-01-06   NaN
2016-01-07   NaN
2016-01-08   NaN
2016-01-09   NaN

In this way calendar heatmap comes out empty. Any idea?

Upvotes: 1

Views: 111

Answers (1)

jezrael
jezrael

Reputation: 862791

It seems you need set_index for creating Series:

df['DateTime'] = pd.to_datetime(df['ISTANTE'], format='%Y%m%d%H%M')
events = df.set_index('DateTime')['VM']
print (events)
DateTime
2016-01-01    0.0
2016-01-02    0.2
2016-01-03    0.6
2016-01-04    1.8
Name: VM, dtype: float64

Reason why dont work:

events = pd.Series((df['VM']), index=df['DateTime'])

is data alignment. Index of column VM (Series) want to align by DatetimeIndex, but values not match, so result is NaNs.

Upvotes: 1

Related Questions