@@ -908,26 +908,8 @@ data columns:
908
908
Date parsing functions
909
909
++++++++++++++++++++++
910
910
911
- Finally, the parser allows you to specify a custom ``date_parser `` function to
912
- take full advantage of the flexibility of the date parsing API:
913
-
914
- .. ipython :: python
915
-
916
- df = pd.read_csv(
917
- " tmp.csv" , header = None , parse_dates = date_spec, date_parser = pd.to_datetime
918
- )
919
- df
920
-
921
- pandas will try to call the ``date_parser `` function in three different ways. If
922
- an exception is raised, the next one is tried:
923
-
924
- 1. ``date_parser `` is first called with one or more arrays as arguments,
925
- as defined using ``parse_dates `` (e.g., ``date_parser(['2013', '2013'], ['1', '2']) ``).
926
-
927
- 2. If #1 fails, ``date_parser `` is called with all the columns
928
- concatenated row-wise into a single array (e.g., ``date_parser(['2013 1', '2013 2']) ``).
929
-
930
- Note that performance-wise, you should try these methods of parsing dates in order:
911
+ Finally, the parser allows you to specify a custom ``date_format ``.
912
+ Performance-wise, you should try these methods of parsing dates in order:
931
913
932
914
1. If you know the format, use ``date_format ``, e.g.:
933
915
``date_format="%d/%m/%Y" ``.
@@ -962,16 +944,13 @@ an object-dtype column with strings, even with ``parse_dates``.
962
944
df = pd.read_csv(StringIO(content), parse_dates = [" a" ])
963
945
df[" a" ]
964
946
965
- To parse the mixed-timezone values as a datetime column, pass a partially-applied
966
- :func: `to_datetime ` with ``utc=True `` as the `` date_parser ``.
947
+ To parse the mixed-timezone values as a datetime column, read in as `` object `` dtype and
948
+ then call :func: `to_datetime ` with ``utc=True ``.
967
949
968
950
.. ipython :: python
969
951
970
- df = pd.read_csv(
971
- StringIO(content),
972
- parse_dates = [" a" ],
973
- date_parser = lambda col : pd.to_datetime(col, utc = True ),
974
- )
952
+ df = pd.read_csv(StringIO(content))
953
+ df[" a" ] = pd.to_datetime(df[" a" ], utc = True )
975
954
df[" a" ]
976
955
977
956
0 commit comments