Read a CSV into a DataFrame with pd.read_csv, parse the dates, and index the rows by date.
A CSV file is a text file with one row per line and commas between the fields. pd.read_csv turns one into a DataFrame.
In this lesson I read prices.csv, a simulated file of daily closes sitting next to this page, check what came in, fix the date column, and pull out one ticker with its rows labelled by date.
Step 1. Read the file
The file has four columns, Date,Ticker,Close,Volume, and three tickers stacked on top of each other. Pass the filename and pandas hands back a DataFrame.
import pandas as pdprices = pd.read_csv("prices.csv") # the file sits in the same folder as this lessonprint(prices.head())# -> Date Ticker Close Volume# -> 0 2026-01-02 AAPL 186.66 2183494# -> 1 2026-01-02 MSFT 416.98 3117814# -> 2 2026-01-02 NVDA 125.18 7447373# -> 3 2026-01-05 AAPL 186.38 8463199# -> 4 2026-01-05 MSFT 423.19 3345056print(prices.shape) # -> (120, 4)
head() shows the first five rows. shape is rows then columns: 120 rows, 4 columns. Three tickers times 40 business days gives 120.
The first line of the file became the column names, and the numbers on the left, 0 to 4, are the index pandas made up because I gave it nothing to use.
Step 2. Date arrives as text
read_csv guesses a type per column. Ask it what it decided.
print(prices.dtypes)# -> Date str# -> Ticker str# -> Close float64# -> Volume int64# -> dtype: object
Date str
Ticker str
Close float64
Volume int64
dtype: object
Close came in as a float and Volume as an integer. Date came in as str, which is text. "2026-01-02" is a string of ten characters, not a date, so the date tools are not available on it.
print(type(prices["Date"][0])) # -> <class 'str'>
<class 'str'>
Pass parse_dates with a list of column names and pandas converts them while reading.
prices = pd.read_csv("prices.csv", parse_dates=["Date"])print(prices.dtypes)# -> Date datetime64[us]# -> Ticker str# -> Close float64# -> Volume int64# -> dtype: objectprint(prices["Date"].min(), "to", prices["Date"].max())# -> 2026-01-02 00:00:00 to 2026-02-26 00:00:00
Date datetime64[us]
Ticker str
Close float64
Volume int64
dtype: object
2026-01-02 00:00:00 to 2026-02-26 00:00:00
datetime64 is a real timestamp. Now .dt reaches into it and pulls out the parts of the date.
idxmax() gives the index label of the largest value, which is a date now that the date is the index.
NoteWhere does pandas look for the file?
pd.read_csv("prices.csv") looks in the working directory, which for a lesson page is the folder the page lives in. If the file sits somewhere else, give the path: pd.read_csv("data/prices.csv"), or the full path. A missing file raises FileNotFoundError naming what it tried to open.