Pandas read csv Custom Date Format

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

Internal

Overview

A CSV column can be parsed as date with:

df = pd.read_csv("./timeseries.csv", parse_dates=["date"])

This assumes a "2023-10-31" format. If the string format is different there are several options:

date_format Parameter

df = pd.read_csv("./timeseries.csv", parse_dates=["date"], date_format='%m/%Y/%d')

More details on format:

datetime Format

The problem with that is that I don't get a series of datetimes, but a series of objects. Why?

date_parser Parameter

For more complicated formats, the parsing function can be provided as a named function or a lambda, and that function can be passed to read_csv with the date_parser parameter.

def parse_timestamp(s: str):
  ???
df = pd.read_csv("./timeseries.csv", parse_dates=["date"], date_format='%m/%Y/%d')


For more details on timestamp parsing see:

Time, Date, Timestamp in Python
df = pd.read_csv("./timeseries.csv", parse_dates=["date"], date_format='%m/%Y/%d')

More details on format:

datetime Format

The problem with that is that I don't get a series of datetimes, but a series of objects. Why?

date_parser Parameter