Skip to content

Commit 54210f3

Browse files
committed
DEPR: unit->input_unit in to_datetime, to_timedelta
1 parent 98c9c7f commit 54210f3

File tree

90 files changed

+684
-459
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+684
-459
lines changed

asv_bench/benchmarks/categoricals.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ def setup(self):
7979
"int": np.random.randint(2**16, size=154),
8080
"float": sys.maxsize * np.random.random((38,)),
8181
"timestamp": [
82-
pd.Timestamp(x, unit="s") for x in np.random.randint(2**18, size=578)
82+
pd.Timestamp(x, input_unit="s")
83+
for x in np.random.randint(2**18, size=578)
8384
],
8485
}
8586

asv_bench/benchmarks/groupby.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ def setup(self):
11301130
index = MultiIndex.from_product(
11311131
[
11321132
np.arange(num_groups),
1133-
to_timedelta(np.arange(num_timedeltas), unit="s"),
1133+
to_timedelta(np.arange(num_timedeltas), input_unit="s"),
11341134
],
11351135
names=["groups", "timedeltas"],
11361136
)

asv_bench/benchmarks/inference.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,22 @@ def setup(self):
118118
# speed of int64, uint64 and float64 paths should be comparable
119119

120120
def time_nanosec_int64(self):
121-
to_datetime(self.ts_nanosec, unit="ns")
121+
to_datetime(self.ts_nanosec, input_unit="ns")
122122

123123
def time_nanosec_uint64(self):
124-
to_datetime(self.ts_nanosec_uint, unit="ns")
124+
to_datetime(self.ts_nanosec_uint, input_unit="ns")
125125

126126
def time_nanosec_float64(self):
127-
to_datetime(self.ts_nanosec_float, unit="ns")
127+
to_datetime(self.ts_nanosec_float, input_unit="ns")
128128

129129
def time_sec_uint64(self):
130-
to_datetime(self.ts_sec_uint, unit="s")
130+
to_datetime(self.ts_sec_uint, input_unit="s")
131131

132132
def time_sec_int64(self):
133-
to_datetime(self.ts_sec, unit="s")
133+
to_datetime(self.ts_sec, input_unit="s")
134134

135135
def time_sec_float64(self):
136-
to_datetime(self.ts_sec_float, unit="s")
136+
to_datetime(self.ts_sec_float, input_unit="s")
137137

138138

139139
class ToDatetimeYYYYMMDD:
@@ -250,10 +250,10 @@ def setup(self, cache):
250250
self.dup_string_with_tz = ["2000-02-11 15:00:00-0800"] * N
251251

252252
def time_unique_seconds_and_unit(self, cache):
253-
to_datetime(self.unique_numeric_seconds, unit="s", cache=cache)
253+
to_datetime(self.unique_numeric_seconds, input_unit="s", cache=cache)
254254

255255
def time_dup_seconds_and_unit(self, cache):
256-
to_datetime(self.dup_numeric_seconds, unit="s", cache=cache)
256+
to_datetime(self.dup_numeric_seconds, input_unit="s", cache=cache)
257257

258258
def time_dup_string_dates(self, cache):
259259
to_datetime(self.dup_string_dates, cache=cache)
@@ -275,7 +275,7 @@ def setup(self):
275275
self.str_seconds.append(f"00:00:{i:02d}")
276276

277277
def time_convert_int(self):
278-
to_timedelta(self.ints, unit="s")
278+
to_timedelta(self.ints, input_unit="s")
279279

280280
def time_convert_string_days(self):
281281
to_timedelta(self.str_days)

asv_bench/benchmarks/tslibs/timedelta.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ class TimedeltaConstructor:
1414
def setup(self):
1515
self.nptimedelta64 = np.timedelta64(3600)
1616
self.dttimedelta = datetime.timedelta(seconds=3600)
17-
self.td = Timedelta(3600, unit="s")
17+
self.td = Timedelta(3600, input_unit="s")
1818

1919
def time_from_int(self):
2020
Timedelta(123456789)
2121

2222
def time_from_unit(self):
23-
Timedelta(1, unit="D")
23+
Timedelta(1, input_unit="D")
2424

2525
def time_from_components(self):
2626
Timedelta(

doc/source/user_guide/timedeltas.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ You can construct a ``Timedelta`` scalar through various arguments, including `I
3535
pd.Timedelta(days=1, seconds=1)
3636
3737
# integers with a unit
38-
pd.Timedelta(1, unit="D")
38+
pd.Timedelta(1, input_unit="D")
3939
4040
# from a datetime.timedelta/np.timedelta64
4141
pd.Timedelta(datetime.timedelta(days=1, seconds=1))
@@ -93,8 +93,8 @@ is numeric:
9393

9494
.. ipython:: python
9595
96-
pd.to_timedelta(np.arange(5), unit="s")
97-
pd.to_timedelta(np.arange(5), unit="D")
96+
pd.to_timedelta(np.arange(5), input_unit="s")
97+
pd.to_timedelta(np.arange(5), input_unit="D")
9898
9999
.. warning::
100100
If a string or array of strings is passed as an input then the ``unit`` keyword
@@ -199,7 +199,7 @@ You can fillna on timedeltas, passing a timedelta to get a particular value.
199199
.. ipython:: python
200200
201201
y.fillna(pd.Timedelta(0))
202-
y.fillna(pd.Timedelta(10, unit="s"))
202+
y.fillna(pd.Timedelta(10, input_unit="s"))
203203
y.fillna(pd.Timedelta("-1 days, 00:00:05"))
204204
205205
You can also negate, multiply and use ``abs`` on ``Timedeltas``:

doc/source/user_guide/timeseries.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,8 @@ as timezone-naive timestamps and then localize to the appropriate timezone:
353353

354354
.. ipython:: python
355355
356-
pd.to_datetime([1490195805.433, 1490195805.433502912], unit="s")
357-
pd.to_datetime(1490195805433502912, unit="ns")
356+
pd.to_datetime([1490195805.433, 1490195805.433502912], input_unit="s")
357+
pd.to_datetime(1490195805433502912, input_unit="ns")
358358
359359
.. seealso::
360360

@@ -389,14 +389,14 @@ of a ``DatetimeIndex``. For example, to use 1960-01-01 as the starting date:
389389

390390
.. ipython:: python
391391
392-
pd.to_datetime([1, 2, 3], unit="D", origin=pd.Timestamp("1960-01-01"))
392+
pd.to_datetime([1, 2, 3], input_unit="D", origin=pd.Timestamp("1960-01-01"))
393393
394394
The default is set at ``origin='unix'``, which defaults to ``1970-01-01 00:00:00``.
395395
Commonly called 'unix epoch' or POSIX time.
396396

397397
.. ipython:: python
398398
399-
pd.to_datetime([1, 2, 3], unit="D")
399+
pd.to_datetime([1, 2, 3], input_unit="D")
400400
401401
.. _timeseries.daterange:
402402

@@ -2633,7 +2633,7 @@ Transform nonexistent times to ``NaT`` or shift the times.
26332633
dti
26342634
dti.tz_localize("Europe/Warsaw", nonexistent="shift_forward")
26352635
dti.tz_localize("Europe/Warsaw", nonexistent="shift_backward")
2636-
dti.tz_localize("Europe/Warsaw", nonexistent=pd.Timedelta(1, unit="h"))
2636+
dti.tz_localize("Europe/Warsaw", nonexistent=pd.Timedelta(1, input_unit="h"))
26372637
dti.tz_localize("Europe/Warsaw", nonexistent="NaT")
26382638
26392639

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ Other Deprecations
667667
- Deprecated allowing ``fill_value`` that cannot be held in the original dtype (excepting NA values for integer and bool dtypes) in :meth:`Series.unstack` and :meth:`DataFrame.unstack` (:issue:`12189`, :issue:`53868`)
668668
- Deprecated allowing ``fill_value`` that cannot be held in the original dtype (excepting NA values for integer and bool dtypes) in :meth:`Series.shift` and :meth:`DataFrame.shift` (:issue:`53802`)
669669
- Deprecated slicing on a :class:`Series` or :class:`DataFrame` with a :class:`DatetimeIndex` using a ``datetime.date`` object, explicitly cast to :class:`Timestamp` instead (:issue:`35830`)
670+
- Deprecated the ``unit`` keyword in :meth:`to_datetime` and :meth:`to_timedelta`, use ``input_unit`` instead (:issue:`62097`)
670671

671672
.. ---------------------------------------------------------------------------
672673
.. _whatsnew_300.prior_deprecations:

pandas/_libs/tslibs/conversion.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def cast_from_unit_vectorized(
117117
# but not clear what 2.5 "M" corresponds to, so we will
118118
# disallow that case.
119119
raise ValueError(
120-
f"Conversion of non-round float with unit={unit} "
120+
f"Conversion of non-round float with input_unit={unit} "
121121
"is ambiguous"
122122
)
123123

@@ -194,7 +194,7 @@ cdef int64_t cast_from_unit(
194194
# but not clear what 2.5 "M" corresponds to, so we will
195195
# disallow that case.
196196
raise ValueError(
197-
f"Conversion of non-round float with unit={unit} "
197+
f"Conversion of non-round float with input_unit={unit} "
198198
"is ambiguous"
199199
)
200200
# GH#47266 go through np.datetime64 to avoid weird results e.g. with "Y"

pandas/_libs/tslibs/offsets.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5382,7 +5382,7 @@ cpdef to_offset(freq, bint is_period=False):
53825382
# For these prefixes, we have something like "3h" or
53835383
# "2.5min", so we can construct a Timedelta with the
53845384
# matching unit and get our offset from delta_to_tick
5385-
td = Timedelta(1, unit=name)
5385+
td = Timedelta(1, input_unit=name)
53865386
off = delta_to_tick(td)
53875387
offset = off * float(stride)
53885388
if n != 0:

pandas/_libs/tslibs/timedeltas.pyx

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ cdef class _Timedelta(timedelta):
11321132
11331133
Examples
11341134
--------
1135-
>>> pd.Timedelta(1, "us").value
1135+
>>> pd.Timedelta(1, input_unit="us").value
11361136
1000
11371137
"""
11381138
try:
@@ -1174,7 +1174,7 @@ cdef class _Timedelta(timedelta):
11741174
11751175
Examples
11761176
--------
1177-
>>> td = pd.Timedelta(1, "d")
1177+
>>> td = pd.Timedelta(1, input_unit="d")
11781178
>>> td.days
11791179
1
11801180
@@ -1216,7 +1216,7 @@ cdef class _Timedelta(timedelta):
12161216
12171217
**Using integer input**
12181218
1219-
>>> td = pd.Timedelta(42, unit='s')
1219+
>>> td = pd.Timedelta(42, input_unit='s')
12201220
>>> td.seconds
12211221
42
12221222
"""
@@ -1256,7 +1256,7 @@ cdef class _Timedelta(timedelta):
12561256
12571257
**Using integer input**
12581258
1259-
>>> td = pd.Timedelta(42, unit='us')
1259+
>>> td = pd.Timedelta(42, input_unit='us')
12601260
>>> td.microseconds
12611261
42
12621262
"""
@@ -1308,7 +1308,8 @@ cdef class _Timedelta(timedelta):
13081308

13091309
Examples
13101310
--------
1311-
>>> td = pd.Timedelta(42, unit='us')
1311+
>>> td = pd.Timedelta(42, input_unit='us')
1312+
>>> ts.unit
13121313
'ns'
13131314
"""
13141315
return npy_unit_to_abbrev(self._creso)
@@ -1652,7 +1653,7 @@ cdef class _Timedelta(timedelta):
16521653
>>> td.asm8
16531654
numpy.timedelta64(3005000,'ns')
16541655

1655-
>>> td = pd.Timedelta(42, unit='ns')
1656+
>>> td = pd.Timedelta(42, input_unit='ns')
16561657
>>> td.asm8
16571658
numpy.timedelta64(42,'ns')
16581659
"""
@@ -1696,7 +1697,7 @@ cdef class _Timedelta(timedelta):
16961697
>>> td.resolution_string
16971698
's'
16981699

1699-
>>> td = pd.Timedelta(36, unit='us')
1700+
>>> td = pd.Timedelta(36, input_unit='us')
17001701
>>> td.resolution_string
17011702
'us'
17021703
"""
@@ -1743,7 +1744,7 @@ cdef class _Timedelta(timedelta):
17431744
17441745
**Using integer input**
17451746
1746-
>>> td = pd.Timedelta(42, unit='ns')
1747+
>>> td = pd.Timedelta(42, input_unit='ns')
17471748
>>> td.nanoseconds
17481749
42
17491750
"""
@@ -1995,7 +1996,7 @@ class Timedelta(_Timedelta):
19951996
--------
19961997
Here we initialize Timedelta object with both value and unit
19971998
1998-
>>> td = pd.Timedelta(1, "D")
1999+
>>> td = pd.Timedelta(1, input_unit="D")
19992000
>>> td
20002001
Timedelta('1 days 00:00:00')
20012002
@@ -2011,7 +2012,18 @@ class Timedelta(_Timedelta):
20112012
_req_any_kwargs_new = {"weeks", "days", "hours", "minutes", "seconds",
20122013
"milliseconds", "microseconds", "nanoseconds"}
20132014

2014-
def __new__(cls, object value=_no_input, unit=None, **kwargs):
2015+
def __new__(cls, object value=_no_input, unit=None, *, input_unit=None, **kwargs):
2016+
if unit is not None:
2017+
if input_unit is not None:
2018+
raise ValueError("Specify only 'input_unit', not 'unit'")
2019+
from pandas.errors import Pandas4Warning
2020+
warnings.warn(
2021+
"The 'unit' keyword is deprecated. Use 'input_unit' instead.",
2022+
Pandas4Warning,
2023+
stacklevel=find_stack_level(),
2024+
)
2025+
input_unit = unit
2026+
20152027
unsupported_kwargs = set(kwargs)
20162028
unsupported_kwargs.difference_update(cls._req_any_kwargs_new)
20172029
if unsupported_kwargs or (
@@ -2066,12 +2078,12 @@ class Timedelta(_Timedelta):
20662078
)
20672079
raise OutOfBoundsTimedelta(msg) from err
20682080

2069-
disallow_ambiguous_unit(unit)
2081+
disallow_ambiguous_unit(input_unit)
20702082

20712083
# GH 30543 if pd.Timedelta already passed, return it
20722084
# check that only value is passed
20732085
if isinstance(value, _Timedelta):
2074-
# 'unit' is benign in this case, but e.g. days or seconds
2086+
# 'input_unit' is benign in this case, but e.g. days or seconds
20752087
# doesn't make sense here.
20762088
if len(kwargs):
20772089
# GH#48898
@@ -2082,8 +2094,10 @@ class Timedelta(_Timedelta):
20822094
)
20832095
return value
20842096
elif isinstance(value, str):
2085-
if unit is not None:
2086-
raise ValueError("unit must not be specified if the value is a str")
2097+
if input_unit is not None:
2098+
raise ValueError(
2099+
"input_unit must not be specified if the value is a str"
2100+
)
20872101
if (len(value) > 0 and value[0] == "P") or (
20882102
len(value) > 1 and value[:2] == "-P"
20892103
):
@@ -2140,8 +2154,8 @@ class Timedelta(_Timedelta):
21402154

21412155
elif is_integer_object(value) or is_float_object(value):
21422156
# unit=None is de-facto 'ns'
2143-
unit = parse_timedelta_unit(unit)
2144-
value = convert_to_timedelta64(value, unit)
2157+
input_unit = parse_timedelta_unit(input_unit)
2158+
value = convert_to_timedelta64(value, input_unit)
21452159
elif checknull_with_nat_and_na(value):
21462160
return NaT
21472161
else:

0 commit comments

Comments
 (0)