Skip to content

Commit 6973ad5

Browse files
authored
DOC: Add examples for pd.read_csv (#58661)
* DOC: Add examples for pd.read_csv * Add double braces * fixes * Add example for date_format * Consistent use of single quotes * I forgot * Add double braces, again.. * Space * Add useful text
1 parent 1556dc0 commit 6973ad5

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

pandas/io/parsers/readers.py

+75
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,81 @@ class _read_shared(TypedDict, Generic[HashableT], total=False):
486486
Examples
487487
--------
488488
>>> pd.{func_name}('data.csv') # doctest: +SKIP
489+
Name Value
490+
0 foo 1
491+
1 bar 2
492+
2 #baz 3
493+
494+
Index and header can be specified via the `index_col` and `header` arguments.
495+
496+
>>> pd.{func_name}('data.csv', header=None) # doctest: +SKIP
497+
0 1
498+
0 Name Value
499+
1 foo 1
500+
2 bar 2
501+
3 #baz 3
502+
503+
>>> pd.{func_name}('data.csv', index_col='Value') # doctest: +SKIP
504+
Name
505+
Value
506+
1 foo
507+
2 bar
508+
3 #baz
509+
510+
Column types are inferred but can be explicitly specified using the dtype argument.
511+
512+
>>> pd.{func_name}('data.csv', dtype={{'Value': float}}) # doctest: +SKIP
513+
Name Value
514+
0 foo 1.0
515+
1 bar 2.0
516+
2 #baz 3.0
517+
518+
True, False, and NA values, and thousands separators have defaults,
519+
but can be explicitly specified, too. Supply the values you would like
520+
as strings or lists of strings!
521+
522+
>>> pd.{func_name}('data.csv', na_values=['foo', 'bar']) # doctest: +SKIP
523+
Name Value
524+
0 NaN 1
525+
1 NaN 2
526+
2 #baz 3
527+
528+
Comment lines in the input file can be skipped using the `comment` argument.
529+
530+
>>> pd.{func_name}('data.csv', comment='#') # doctest: +SKIP
531+
Name Value
532+
0 foo 1
533+
1 bar 2
534+
535+
By default, columns with dates will be read as ``object`` rather than ``datetime``.
536+
537+
>>> df = pd.{func_name}('tmp.csv') # doctest: +SKIP
538+
539+
>>> df # doctest: +SKIP
540+
col 1 col 2 col 3
541+
0 10 10/04/2018 Sun 15 Jan 2023
542+
1 20 15/04/2018 Fri 12 May 2023
543+
544+
>>> df.dtypes # doctest: +SKIP
545+
col 1 int64
546+
col 2 object
547+
col 3 object
548+
dtype: object
549+
550+
Specific columns can be parsed as dates by using the `parse_dates` and
551+
`date_format` arguments.
552+
553+
>>> df = pd.{func_name}(
554+
... 'tmp.csv',
555+
... parse_dates=[1, 2],
556+
... date_format={{'col 2': '%d/%m/%Y', 'col 3': '%a %d %b %Y'}},
557+
... ) # doctest: +SKIP
558+
559+
>>> df.dtypes # doctest: +SKIP
560+
col 1 int64
561+
col 2 datetime64[ns]
562+
col 3 datetime64[ns]
563+
dtype: object
489564
"""
490565
)
491566

0 commit comments

Comments
 (0)