Visualizing Time Series Data in Python
Thomas Vincent
Head of Data Science, Getty Images
import pandas as pd
df = pd.read_csv('ch2_co2_levels.csv') print(df)
datestamp co2
0 1958-03-29 316.1
1 1958-04-05 317.3
2 1958-04-12 317.6
...
...
...
2281 2001-12-15 371.2
2282 2001-12-22 371.3
2283 2001-12-29 371.5
print(df.head(n=5))
datestamp co2
0 1958-03-29 316.1
1 1958-04-05 317.3
2 1958-04-12 317.6
3 1958-04-19 317.5
4 1958-04-26 316.4
print(df.tail(n=5))
datestamp co2
2279 2001-12-01 370.3
2280 2001-12-08 370.8
2281 2001-12-15 371.2
2282 2001-12-22 371.3
2283 2001-12-29 371.5
print(df.dtypes)
datestamp object
co2 float64
dtype: object
To work with time series data in pandas
, your date columns needs to be of the datetime64
type.
pd.to_datetime(['2009/07/31', 'test'])
ValueError: Unknown string format
pd.to_datetime(['2009/07/31', 'test'], errors='coerce')
DatetimeIndex(['2009-07-31', 'NaT'],
dtype='datetime64[ns]', freq=None)
Visualizing Time Series Data in Python