14
14
parse_time_string ,
15
15
)
16
16
from pandas ._libs .tslibs .strptime import array_strptime
17
- from pandas .util ._decorators import deprecate_kwarg
18
17
19
18
from pandas .core .dtypes .common import (
20
19
ensure_object ,
45
44
# types used in annotations
46
45
47
46
ArrayConvertible = Union [list , tuple , ArrayLike , ABCSeries ]
48
-
49
- # ---------------------------------------------------------------------
50
-
51
- # ---------------------------------------------------------------------
52
- # types used in annotations
53
-
54
47
Scalar = Union [int , float , str ]
55
48
DatetimeScalar = TypeVar ("DatetimeScalar" , Scalar , datetime )
56
49
DatetimeScalarOrArrayConvertible = Union [
@@ -154,7 +147,7 @@ def _maybe_cache(arg, format, cache, convert_listlike):
154
147
155
148
unique_dates = unique (arg )
156
149
if len (unique_dates ) < len (arg ):
157
- cache_dates = convert_listlike (unique_dates , True , format )
150
+ cache_dates = convert_listlike (unique_dates , format )
158
151
cache_array = Series (cache_dates , index = unique_dates )
159
152
return cache_array
160
153
@@ -169,7 +162,7 @@ def _box_as_indexlike(
169
162
Parameters
170
163
----------
171
164
dt_array: 1-d array
172
- array of datetimes to be boxed
165
+ Array of datetimes to be wrapped in an Index.
173
166
tz : object
174
167
None or 'utc'
175
168
name : string, default None
@@ -192,37 +185,30 @@ def _box_as_indexlike(
192
185
def _convert_and_box_cache (
193
186
arg : DatetimeScalarOrArrayConvertible ,
194
187
cache_array : ABCSeries ,
195
- box : bool ,
196
188
name : Optional [str ] = None ,
197
- ) -> Union [ ABCIndex , np . ndarray ] :
189
+ ) -> ABCIndexClass :
198
190
"""
199
- Convert array of dates with a cache and box the result
191
+ Convert array of dates with a cache and wrap the result in an Index.
200
192
201
193
Parameters
202
194
----------
203
195
arg : integer, float, string, datetime, list, tuple, 1-d array, Series
204
196
cache_array : Series
205
197
Cache of converted, unique dates
206
- box : boolean
207
- True boxes result as an Index-like, False returns an ndarray
208
198
name : string, default None
209
199
Name for a DatetimeIndex
210
200
211
201
Returns
212
202
-------
213
- result : datetime of converted dates
214
- - Index-like if box=True
215
- - ndarray if box=False
203
+ result : Index-like of converted dates
216
204
"""
217
205
from pandas import Series
218
206
219
207
result = Series (arg ).map (cache_array )
220
- if box :
221
- return _box_as_indexlike (result , utc = None , name = name )
222
- return result .values
208
+ return _box_as_indexlike (result , utc = None , name = name )
223
209
224
210
225
- def _return_parsed_timezone_results (result , timezones , box , tz , name ):
211
+ def _return_parsed_timezone_results (result , timezones , tz , name ):
226
212
"""
227
213
Return results from array_strptime if a %z or %Z directive was passed.
228
214
@@ -232,20 +218,14 @@ def _return_parsed_timezone_results(result, timezones, box, tz, name):
232
218
int64 date representations of the dates
233
219
timezones : ndarray
234
220
pytz timezone objects
235
- box : boolean
236
- True boxes result as an Index-like, False returns an ndarray
237
221
tz : object
238
222
None or pytz timezone object
239
223
name : string, default None
240
224
Name for a DatetimeIndex
241
225
242
226
Returns
243
227
-------
244
- tz_result : ndarray of parsed dates with timezone
245
- Returns:
246
-
247
- - Index-like if box=True
248
- - ndarray of Timestamps if box=False
228
+ tz_result : Index-like of parsed dates with timezone
249
229
"""
250
230
if tz is not None :
251
231
raise ValueError (
@@ -256,16 +236,13 @@ def _return_parsed_timezone_results(result, timezones, box, tz, name):
256
236
tz_results = np .array (
257
237
[Timestamp (res ).tz_localize (zone ) for res , zone in zip (result , timezones )]
258
238
)
259
- if box :
260
- from pandas import Index
239
+ from pandas import Index
261
240
262
- return Index (tz_results , name = name )
263
- return tz_results
241
+ return Index (tz_results , name = name )
264
242
265
243
266
244
def _convert_listlike_datetimes (
267
245
arg ,
268
- box ,
269
246
format ,
270
247
name = None ,
271
248
tz = None ,
@@ -284,8 +261,6 @@ def _convert_listlike_datetimes(
284
261
----------
285
262
arg : list, tuple, ndarray, Series, Index
286
263
date to be parced
287
- box : boolean
288
- True boxes result as an Index-like, False returns an ndarray
289
264
name : object
290
265
None or string for the Index name
291
266
tz : object
@@ -305,11 +280,7 @@ def _convert_listlike_datetimes(
305
280
306
281
Returns
307
282
-------
308
- ndarray of parsed dates
309
- Returns:
310
-
311
- - Index-like if box=True
312
- - ndarray of Timestamps if box=False
283
+ Index-like of parsed dates
313
284
"""
314
285
from pandas import DatetimeIndex
315
286
from pandas .core .arrays import DatetimeArray
@@ -330,7 +301,7 @@ def _convert_listlike_datetimes(
330
301
return arg
331
302
332
303
elif is_datetime64_ns_dtype (arg ):
333
- if box and not isinstance (arg , (DatetimeArray , DatetimeIndex )):
304
+ if not isinstance (arg , (DatetimeArray , DatetimeIndex )):
334
305
try :
335
306
return DatetimeIndex (arg , tz = tz , name = name )
336
307
except ValueError :
@@ -346,26 +317,25 @@ def _convert_listlike_datetimes(
346
317
raise ValueError ("cannot specify both format and unit" )
347
318
arg = getattr (arg , "values" , arg )
348
319
result , tz_parsed = tslib .array_with_unit_to_datetime (arg , unit , errors = errors )
349
- if box :
350
- if errors == "ignore" :
351
- from pandas import Index
320
+ if errors == "ignore" :
321
+ from pandas import Index
352
322
353
- result = Index (result , name = name )
323
+ result = Index (result , name = name )
324
+ else :
325
+ result = DatetimeIndex (result , name = name )
326
+ # GH 23758: We may still need to localize the result with tz
327
+ # GH 25546: Apply tz_parsed first (from arg), then tz (from caller)
328
+ # result will be naive but in UTC
329
+ try :
330
+ result = result .tz_localize ("UTC" ).tz_convert (tz_parsed )
331
+ except AttributeError :
332
+ # Regular Index from 'ignore' path
333
+ return result
334
+ if tz is not None :
335
+ if result .tz is None :
336
+ result = result .tz_localize (tz )
354
337
else :
355
- result = DatetimeIndex (result , name = name )
356
- # GH 23758: We may still need to localize the result with tz
357
- # GH 25546: Apply tz_parsed first (from arg), then tz (from caller)
358
- # result will be naive but in UTC
359
- try :
360
- result = result .tz_localize ("UTC" ).tz_convert (tz_parsed )
361
- except AttributeError :
362
- # Regular Index from 'ignore' path
363
- return result
364
- if tz is not None :
365
- if result .tz is None :
366
- result = result .tz_localize (tz )
367
- else :
368
- result = result .tz_convert (tz )
338
+ result = result .tz_convert (tz )
369
339
return result
370
340
elif getattr (arg , "ndim" , 1 ) > 1 :
371
341
raise TypeError (
@@ -416,7 +386,7 @@ def _convert_listlike_datetimes(
416
386
)
417
387
if "%Z" in format or "%z" in format :
418
388
return _return_parsed_timezone_results (
419
- result , timezones , box , tz , name
389
+ result , timezones , tz , name
420
390
)
421
391
except tslibs .OutOfBoundsDatetime :
422
392
if errors == "raise" :
@@ -463,20 +433,12 @@ def _convert_listlike_datetimes(
463
433
)
464
434
465
435
if tz_parsed is not None :
466
- if box :
467
- # We can take a shortcut since the datetime64 numpy array
468
- # is in UTC
469
- return DatetimeIndex ._simple_new (result , name = name , tz = tz_parsed )
470
- else :
471
- # Convert the datetime64 numpy array to an numpy array
472
- # of datetime objects
473
- result = [Timestamp (ts , tz = tz_parsed ).to_pydatetime () for ts in result ]
474
- return np .array (result , dtype = object )
436
+ # We can take a shortcut since the datetime64 numpy array
437
+ # is in UTC
438
+ return DatetimeIndex ._simple_new (result , name = name , tz = tz_parsed )
475
439
476
- if box :
477
- utc = tz == "utc"
478
- return _box_as_indexlike (result , utc = utc , name = name )
479
- return result
440
+ utc = tz == "utc"
441
+ return _box_as_indexlike (result , utc = utc , name = name )
480
442
481
443
482
444
def _adjust_to_origin (arg , origin , unit ):
@@ -558,14 +520,12 @@ def _adjust_to_origin(arg, origin, unit):
558
520
return arg
559
521
560
522
561
- @deprecate_kwarg (old_arg_name = "box" , new_arg_name = None )
562
523
def to_datetime (
563
524
arg ,
564
525
errors = "raise" ,
565
526
dayfirst = False ,
566
527
yearfirst = False ,
567
528
utc = None ,
568
- box = True ,
569
529
format = None ,
570
530
exact = True ,
571
531
unit = None ,
@@ -603,15 +563,6 @@ def to_datetime(
603
563
utc : bool, default None
604
564
Return UTC DatetimeIndex if True (converting any tz-aware
605
565
datetime.datetime objects as well).
606
- box : bool, default True
607
- - If True returns a DatetimeIndex or Index-like object
608
- - If False returns ndarray of values.
609
-
610
- .. deprecated:: 0.25.0
611
- Use :meth:`Series.to_numpy` or :meth:`Timestamp.to_datetime64`
612
- instead to get an ndarray of values or numpy.datetime64,
613
- respectively.
614
-
615
566
format : str, default None
616
567
The strftime to parse time, eg "%d/%m/%Y", note that "%f" will parse
617
568
all the way up to nanoseconds.
@@ -764,25 +715,25 @@ def to_datetime(
764
715
if not cache_array .empty :
765
716
result = arg .map (cache_array )
766
717
else :
767
- values = convert_listlike (arg ._values , True , format )
718
+ values = convert_listlike (arg ._values , format )
768
719
result = arg ._constructor (values , index = arg .index , name = arg .name )
769
720
elif isinstance (arg , (ABCDataFrame , abc .MutableMapping )):
770
- result = _assemble_from_unit_mappings (arg , errors , box , tz )
721
+ result = _assemble_from_unit_mappings (arg , errors , tz )
771
722
elif isinstance (arg , ABCIndexClass ):
772
723
cache_array = _maybe_cache (arg , format , cache , convert_listlike )
773
724
if not cache_array .empty :
774
- result = _convert_and_box_cache (arg , cache_array , box , name = arg .name )
725
+ result = _convert_and_box_cache (arg , cache_array , name = arg .name )
775
726
else :
776
727
convert_listlike = partial (convert_listlike , name = arg .name )
777
- result = convert_listlike (arg , box , format )
728
+ result = convert_listlike (arg , format )
778
729
elif is_list_like (arg ):
779
730
cache_array = _maybe_cache (arg , format , cache , convert_listlike )
780
731
if not cache_array .empty :
781
- result = _convert_and_box_cache (arg , cache_array , box )
732
+ result = _convert_and_box_cache (arg , cache_array )
782
733
else :
783
- result = convert_listlike (arg , box , format )
734
+ result = convert_listlike (arg , format )
784
735
else :
785
- result = convert_listlike (np .array ([arg ]), box , format )[0 ]
736
+ result = convert_listlike (np .array ([arg ]), format )[0 ]
786
737
787
738
return result
788
739
@@ -813,7 +764,7 @@ def to_datetime(
813
764
}
814
765
815
766
816
- def _assemble_from_unit_mappings (arg , errors , box , tz ):
767
+ def _assemble_from_unit_mappings (arg , errors , tz ):
817
768
"""
818
769
assemble the unit specified fields from the arg (DataFrame)
819
770
Return a Series for actual parsing
@@ -826,10 +777,6 @@ def _assemble_from_unit_mappings(arg, errors, box, tz):
826
777
- If 'raise', then invalid parsing will raise an exception
827
778
- If 'coerce', then invalid parsing will be set as NaT
828
779
- If 'ignore', then invalid parsing will return the input
829
- box : boolean
830
-
831
- - If True, return a DatetimeIndex
832
- - If False, return an array
833
780
tz : None or 'utc'
834
781
835
782
Returns
@@ -904,8 +851,6 @@ def coerce(values):
904
851
"cannot assemble the datetimes [{value}]: "
905
852
"{error}" .format (value = value , error = e )
906
853
)
907
- if not box :
908
- return values .values
909
854
return values
910
855
911
856
0 commit comments