1
1
from collections import abc
2
- from datetime import datetime , time
2
+ from datetime import datetime
3
3
from functools import partial
4
4
from itertools import islice
5
- from typing import List , Optional , TypeVar , Union
5
+ from typing import TYPE_CHECKING , Optional , TypeVar , Union
6
+ import warnings
6
7
7
8
import numpy as np
8
9
28
29
is_numeric_dtype ,
29
30
is_scalar ,
30
31
)
31
- from pandas .core .dtypes .generic import (
32
- ABCDataFrame ,
33
- ABCDatetimeIndex ,
34
- ABCIndex ,
35
- ABCIndexClass ,
36
- ABCSeries ,
37
- )
32
+ from pandas .core .dtypes .generic import ABCDataFrame , ABCSeries
38
33
from pandas .core .dtypes .missing import notna
39
34
40
35
from pandas .arrays import DatetimeArray , IntegerArray
41
36
from pandas .core import algorithms
42
37
from pandas .core .algorithms import unique
43
- from pandas .core .arrays .datetimes import tz_to_dtype
38
+ from pandas .core .arrays .datetimes import (
39
+ maybe_convert_dtype ,
40
+ objects_to_datetime64ns ,
41
+ tz_to_dtype ,
42
+ )
43
+ from pandas .core .indexes .base import Index
44
+ from pandas .core .indexes .datetimes import DatetimeIndex
45
+
46
+ if TYPE_CHECKING :
47
+ from pandas import Series # noqa:F401
44
48
45
49
# ---------------------------------------------------------------------
46
50
# types used in annotations
47
51
48
- ArrayConvertible = Union [list , tuple , ArrayLike , ABCSeries ]
52
+ ArrayConvertible = Union [list , tuple , ArrayLike , "Series" ]
49
53
Scalar = Union [int , float , str ]
50
54
DatetimeScalar = TypeVar ("DatetimeScalar" , Scalar , datetime )
51
55
DatetimeScalarOrArrayConvertible = Union [
52
- DatetimeScalar , list , tuple , ArrayLike , ABCSeries
56
+ DatetimeScalar , list , tuple , ArrayLike , "Series"
53
57
]
54
58
55
59
@@ -156,7 +160,7 @@ def _maybe_cache(arg, format, cache, convert_listlike):
156
160
157
161
def _box_as_indexlike (
158
162
dt_array : ArrayLike , utc : Optional [bool ] = None , name : Optional [str ] = None
159
- ) -> Union [ ABCIndex , ABCDatetimeIndex ] :
163
+ ) -> Index :
160
164
"""
161
165
Properly boxes the ndarray of datetimes to DatetimeIndex
162
166
if it is possible or to generic Index instead
@@ -176,7 +180,6 @@ def _box_as_indexlike(
176
180
- DatetimeIndex if convertible to sole datetime64 type
177
181
- general Index otherwise
178
182
"""
179
- from pandas import DatetimeIndex , Index
180
183
181
184
if is_datetime64_dtype (dt_array ):
182
185
tz = "utc" if utc else None
@@ -186,9 +189,9 @@ def _box_as_indexlike(
186
189
187
190
def _convert_and_box_cache (
188
191
arg : DatetimeScalarOrArrayConvertible ,
189
- cache_array : ABCSeries ,
192
+ cache_array : "Series" ,
190
193
name : Optional [str ] = None ,
191
- ) -> ABCIndexClass :
194
+ ) -> "Index" :
192
195
"""
193
196
Convert array of dates with a cache and wrap the result in an Index.
194
197
@@ -235,7 +238,6 @@ def _return_parsed_timezone_results(result, timezones, tz, name):
235
238
if tz is not None :
236
239
# Convert to the same tz
237
240
tz_results = np .array ([tz_result .tz_convert (tz ) for tz_result in tz_results ])
238
- from pandas import Index
239
241
240
242
return Index (tz_results , name = name )
241
243
@@ -281,11 +283,6 @@ def _convert_listlike_datetimes(
281
283
-------
282
284
Index-like of parsed dates
283
285
"""
284
- from pandas import DatetimeIndex
285
- from pandas .core .arrays .datetimes import (
286
- maybe_convert_dtype ,
287
- objects_to_datetime64ns ,
288
- )
289
286
290
287
if isinstance (arg , (list , tuple )):
291
288
arg = np .array (arg , dtype = "O" )
@@ -332,7 +329,6 @@ def _convert_listlike_datetimes(
332
329
)
333
330
334
331
if errors == "ignore" :
335
- from pandas import Index
336
332
337
333
result = Index (result , name = name )
338
334
else :
@@ -366,8 +362,6 @@ def _convert_listlike_datetimes(
366
362
result = np .array (["NaT" ], dtype = "datetime64[ns]" ).repeat (len (arg ))
367
363
return DatetimeIndex (result , name = name )
368
364
elif errors == "ignore" :
369
- from pandas import Index
370
-
371
365
result = Index (arg , name = name )
372
366
return result
373
367
raise
@@ -539,9 +533,7 @@ def _adjust_to_origin(arg, origin, unit):
539
533
offset = offset // tslibs .Timedelta (1 , unit = unit )
540
534
541
535
# scalars & ndarray-like can handle the addition
542
- if is_list_like (arg ) and not isinstance (
543
- arg , (ABCSeries , ABCIndexClass , np .ndarray )
544
- ):
536
+ if is_list_like (arg ) and not isinstance (arg , (ABCSeries , Index , np .ndarray )):
545
537
arg = np .asarray (arg )
546
538
arg = arg + offset
547
539
return arg
@@ -749,7 +741,7 @@ def to_datetime(
749
741
result = arg ._constructor (values , index = arg .index , name = arg .name )
750
742
elif isinstance (arg , (ABCDataFrame , abc .MutableMapping )):
751
743
result = _assemble_from_unit_mappings (arg , errors , tz )
752
- elif isinstance (arg , ABCIndexClass ):
744
+ elif isinstance (arg , Index ):
753
745
cache_array = _maybe_cache (arg , format , cache , convert_listlike )
754
746
if not cache_array .empty :
755
747
result = _convert_and_box_cache (arg , cache_array , name = arg .name )
@@ -944,131 +936,14 @@ def calc_with_mask(carg, mask):
944
936
return None
945
937
946
938
947
- # Fixed time formats for time parsing
948
- _time_formats = [
949
- "%H:%M" ,
950
- "%H%M" ,
951
- "%I:%M%p" ,
952
- "%I%M%p" ,
953
- "%H:%M:%S" ,
954
- "%H%M%S" ,
955
- "%I:%M:%S%p" ,
956
- "%I%M%S%p" ,
957
- ]
958
-
959
-
960
- def _guess_time_format_for_array (arr ):
961
- # Try to guess the format based on the first non-NaN element
962
- non_nan_elements = notna (arr ).nonzero ()[0 ]
963
- if len (non_nan_elements ):
964
- element = arr [non_nan_elements [0 ]]
965
- for time_format in _time_formats :
966
- try :
967
- datetime .strptime (element , time_format )
968
- return time_format
969
- except ValueError :
970
- pass
971
-
972
- return None
973
-
974
-
975
939
def to_time (arg , format = None , infer_time_format = False , errors = "raise" ):
976
- """
977
- Parse time strings to time objects using fixed strptime formats ("%H:%M",
978
- "%H%M", "%I:%M%p", "%I%M%p", "%H:%M:%S", "%H%M%S", "%I:%M:%S%p",
979
- "%I%M%S%p")
980
-
981
- Use infer_time_format if all the strings are in the same format to speed
982
- up conversion.
983
-
984
- Parameters
985
- ----------
986
- arg : string in time format, datetime.time, list, tuple, 1-d array, Series
987
- format : str, default None
988
- Format used to convert arg into a time object. If None, fixed formats
989
- are used.
990
- infer_time_format: bool, default False
991
- Infer the time format based on the first non-NaN element. If all
992
- strings are in the same format, this will speed up conversion.
993
- errors : {'ignore', 'raise', 'coerce'}, default 'raise'
994
- - If 'raise', then invalid parsing will raise an exception
995
- - If 'coerce', then invalid parsing will be set as None
996
- - If 'ignore', then invalid parsing will return the input
997
-
998
- Returns
999
- -------
1000
- datetime.time
1001
- """
1002
-
1003
- def _convert_listlike (arg , format ):
1004
-
1005
- if isinstance (arg , (list , tuple )):
1006
- arg = np .array (arg , dtype = "O" )
1007
-
1008
- elif getattr (arg , "ndim" , 1 ) > 1 :
1009
- raise TypeError (
1010
- "arg must be a string, datetime, list, tuple, 1-d array, or Series"
1011
- )
1012
-
1013
- arg = ensure_object (arg )
1014
-
1015
- if infer_time_format and format is None :
1016
- format = _guess_time_format_for_array (arg )
1017
-
1018
- times : List [Optional [time ]] = []
1019
- if format is not None :
1020
- for element in arg :
1021
- try :
1022
- times .append (datetime .strptime (element , format ).time ())
1023
- except (ValueError , TypeError ) as err :
1024
- if errors == "raise" :
1025
- msg = (
1026
- f"Cannot convert { element } to a time with given "
1027
- f"format { format } "
1028
- )
1029
- raise ValueError (msg ) from err
1030
- elif errors == "ignore" :
1031
- return arg
1032
- else :
1033
- times .append (None )
1034
- else :
1035
- formats = _time_formats [:]
1036
- format_found = False
1037
- for element in arg :
1038
- time_object = None
1039
- for time_format in formats :
1040
- try :
1041
- time_object = datetime .strptime (element , time_format ).time ()
1042
- if not format_found :
1043
- # Put the found format in front
1044
- fmt = formats .pop (formats .index (time_format ))
1045
- formats .insert (0 , fmt )
1046
- format_found = True
1047
- break
1048
- except (ValueError , TypeError ):
1049
- continue
1050
-
1051
- if time_object is not None :
1052
- times .append (time_object )
1053
- elif errors == "raise" :
1054
- raise ValueError (f"Cannot convert arg { arg } to a time" )
1055
- elif errors == "ignore" :
1056
- return arg
1057
- else :
1058
- times .append (None )
1059
-
1060
- return times
1061
-
1062
- if arg is None :
1063
- return arg
1064
- elif isinstance (arg , time ):
1065
- return arg
1066
- elif isinstance (arg , ABCSeries ):
1067
- values = _convert_listlike (arg ._values , format )
1068
- return arg ._constructor (values , index = arg .index , name = arg .name )
1069
- elif isinstance (arg , ABCIndexClass ):
1070
- return _convert_listlike (arg , format )
1071
- elif is_list_like (arg ):
1072
- return _convert_listlike (arg , format )
940
+ # GH#34145
941
+ warnings .warn (
942
+ "`to_time` has been moved, should be imported from pandas.core.tools.times. "
943
+ "This alias will be removed in a future version." ,
944
+ FutureWarning ,
945
+ stacklevel = 2 ,
946
+ )
947
+ from pandas .core .tools .times import to_time
1073
948
1074
- return _convert_listlike ( np . array ([ arg ]) , format )[ 0 ]
949
+ return to_time ( arg , format , infer_time_format , errors )
0 commit comments