Hun3u5ek
Hun3u5ek

Reputation: 1

How to select a specific date range in a csv file with pandas?

I am new to Python playing around with a csv file. I would like to find a way to print out my graph by selecting a specific date range, for example 2013-03-20:2014-03-04.

Code below:

import pandas as pd
import matplotlib.pyplot as plt

prc=pd.read_csv("csv",parse_dates=True, nrows=150, usecols=["Close"])

prc_ma=prc.rolling(5).mean()


plt.plot(prc, color="blue", label="Price")
plt.plot(prc_ma, color="red", label="Moving Average")
plt.xlabel("Date")
plt.ylabel("Price")
plt.title("Moving Average")
plt.grid()

I currently work with the parameter nrows.

Thank you

Upvotes: 0

Views: 5124

Answers (1)

Parfait
Parfait

Reputation: 107652

Simply filter for the dates with .loc assuming datetimes are the index of dataframe:

prc = pd.read_csv("csv", parse_dates=True, nrows=150, usecols=["Close"])

prc_sub = prc.loc['2013-03-20':'2014-03-04']

To demonstrate with random data subsetted out of all days of 2013 and 2014:

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

pd.set_option('display.width', 1000)
np.random.seed(1001)

prc = pd.DataFrame({'PRICE': abs(np.random.randn(730))}, 
                    index=pd.date_range("2013-01-01", "2014-12-31", freq="D"))

# SUBSETTED DATAFRAME
prc_sub = prc.loc['2013-03-20':'2014-03-04']

prc_ma = prc_sub.rolling(5).mean()

plt.plot(prc_sub, color="blue", label="Price")
plt.plot(prc_ma, color="red", label="Moving Average")
plt.xlabel("Date")
plt.ylabel("Price")
plt.title("Moving Average")
plt.grid()

Plot Output

Upvotes: 1

Related Questions