Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Reading and Visualizing Data | Time Series Processing
Time Series Analysis

book
Reading and Visualizing Data

The first thing to start with is reading the data. When working with time series, the rules of the game do not change - you can still use pandas to get data from csv files.

In the files, let's say you have a Date column that contains dates in str type. For further time series analysis, you must turn the str type into a datetime. This is implemented using the pandas function to_datetime()

Let's take the dataset air_quality_no2_long.csv as an example:

python
dataset = pd.read_csv("daily-total-female-births.csv")

Next, we convert the data type in the Date column from str to datetime:

python
dataset["Date"] = pd.to_datetime(dataset["Date"])

You can also do this immediately when reading the dataset:

python
dataset = pd.read_csv("daily-total-female-births.csv", parse_dates=["Date"])

Now we can plot our dataset:

python
fig, ax = plt.subplots(figsize=(11, 9))
ax.plot(dataset["Date"], dataset["Births"])
ax.set_xlabel("Datetime")
ax.set_ylabel("Births")
plt.show()
Task

Swipe to start coding

Read and visualize the AirPassengers.csv dataset.

  1. Import matplotlib.pyplot as plt.
  2. Read the csv file and save it within the data variable.
  3. Convert "Month" into datetime type.
  4. Initialize a line plot with the "Month" column of data on the x-axis and "#Passengers" on the y-axis.
  5. Set labels on an axis and display the plot:
  • "Month" on the x-axis;
  • "Passengers" on the y-axis.

Solution

# Loading libraries
import pandas as pd
import matplotlib.pyplot as plt

# Read dataset and transform data type
data = pd.read_csv("https://codefinity-content-media.s3.eu-west-1.amazonaws.com/943e906e-4de6-4694-a1df-313ceed7cfe7/AirPassengers.csv")
data["Month"] = pd.to_datetime(data["Month"])

# Creating Figure and Axes objects
fig, ax = plt.subplots(figsize=(7, 3))

# Initialize the plot
ax.plot(data["Month"], data["#Passengers"])

# Set axis labels
ax.set_xlabel("Month")
ax.set_ylabel("Passengers")

# Display the plot
plt.show()

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 1
# Loading libraries
import pandas as pd
import ___

# Read dataset and transform data type
data = pd.___("https://codefinity-content-media.s3.eu-west-1.amazonaws.com/943e906e-4de6-4694-a1df-313ceed7cfe7/AirPassengers.csv")
data["Month"] = pd.___(data["Month"])

# Creating Figure and Axes objects
fig, ax = plt.subplots(figsize=(7, 3))

# Initialize the plot
ax.___(___, data["#Passengers"])

# Set axis labels
ax.set_xlabel("___")
ax.set_ylabel("___")

# Display the plot
plt.___()

Ask AI

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt