Pandas read csv Custom Date Format: Difference between revisions
Jump to navigation
Jump to search
Line 18: | Line 18: | ||
==Custom Date == | ==Custom Date == | ||
<syntaxhighlight lang='py'> | <syntaxhighlight lang='py'> | ||
df = pd.read_csv("./timeseries.csv", parse_dates=["date"], date_format='%m/%Y/%d') | df = pd.read_csv("./timeseries.csv", parse_dates=["date"], date_format='%m/%Y/%d') | ||
</syntaxhighlight> | </syntaxhighlight> | ||
More details on format: {{Internal| | |||
The common timestamp elements are '%Y-%m-%d %H:%M:%S'. <font color=darkkhaki>For more details on date format, see ?</font> | The common timestamp elements are '%Y-%m-%d %H:%M:%S'. <font color=darkkhaki>For more details on date format, see ?</font> | ||
Revision as of 01:30, 9 October 2023
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
date_parser Parameter
Custom Date
df = pd.read_csv("./timeseries.csv", parse_dates=["date"], date_format='%m/%Y/%d')
More details on format: {{Internal| 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: