Pandas read csv Custom Date Format: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * Time Series Processing with Pandas =<tt>date_format</tt> Parameter= =<tt>date_parser</tt> Parameter=...")
 
Line 2: Line 2:


* [[Time_Series_Processing_with_Pandas#Custom_Date_Format|Time Series Processing with Pandas]]
* [[Time_Series_Processing_with_Pandas#Custom_Date_Format|Time Series Processing with Pandas]]
=Overview=
A CSV column can be parsed as date with:


=<tt>date_format</tt> Parameter=
=<tt>date_format</tt> Parameter=

Revision as of 00:37, 9 October 2023

Internal

Overview

A CSV column can be parsed as date with:

date_format Parameter

date_parser Parameter

Custom Date

This syntax assumes that the "date" column is encoded in the default Panda date format ('YYYY-MM-DD'). If that is not the case, the format can be specified with the date_format parameters, as shown below:

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

The common timestamp elements are '%Y-%m-%d %H:%M:%S'. For more details on date format, see ?

For more complicated formats, the parsing function can be provided as a named function or a lambda:

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