Skip to content

Commit d6bd938

Browse files
committed
allow DatetimeArray._scalar_from_string to return NaT for blank string
1 parent 337d40e commit d6bd938

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ Other Deprecations
446446
- Deprecated allowing non-keyword arguments in :meth:`Series.to_string` except ``buf``. (:issue:`57280`)
447447
- Deprecated behavior of :meth:`.DataFrameGroupBy.groups` and :meth:`.SeriesGroupBy.groups`, in a future version ``groups`` by one element list will return tuple instead of scalar. (:issue:`58858`)
448448
- Deprecated behavior of :meth:`Series.dt.to_pytimedelta`, in a future version this will return a :class:`Series` containing python ``datetime.timedelta`` objects instead of an ``ndarray`` of timedelta; this matches the behavior of other :meth:`Series.dt` properties. (:issue:`57463`)
449+
- Deprecated empty string in :class:`Timestamp` (:issue:`61149`)
449450
- Deprecated lowercase strings ``d``, ``b`` and ``c`` denoting frequencies in :class:`Day`, :class:`BusinessDay` and :class:`CustomBusinessDay` in favour of ``D``, ``B`` and ``C`` (:issue:`58998`)
450451
- Deprecated lowercase strings ``w``, ``w-mon``, ``w-tue``, etc. denoting frequencies in :class:`Week` in favour of ``W``, ``W-MON``, ``W-TUE``, etc. (:issue:`58998`)
451452
- Deprecated parameter ``method`` in :meth:`DataFrame.reindex_like` / :meth:`Series.reindex_like` (:issue:`58667`)

pandas/_libs/tslibs/timestamps.pyx

+7
Original file line numberDiff line numberDiff line change
@@ -2657,6 +2657,13 @@ class Timestamp(_Timestamp):
26572657
tzinfo is None):
26582658
return ts_input
26592659
elif isinstance(ts_input, str):
2660+
if ts_input == "":
2661+
warnings.warn(
2662+
"Passing an empty string to Timestamp is deprecated and will raise "
2663+
"a ValueError in a future version. Use `pd.NaT` directly instead.",
2664+
FutureWarning,
2665+
stacklevel = find_stack_level()
2666+
)
26602667
# User passed a date string to parse.
26612668
# Check that the user didn't also pass a date attribute kwarg.
26622669
if any(arg is not None for arg in _date_attributes):

pandas/core/arrays/datetimes.py

+2
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,8 @@ def _unbox_scalar(self, value) -> np.datetime64:
547547
return value.as_unit(self.unit, round_ok=False).asm8
548548

549549
def _scalar_from_string(self, value) -> Timestamp | NaTType:
550+
if value == "":
551+
value = NaT
550552
return Timestamp(value, tz=self.tz)
551553

552554
def _check_compatible_with(self, other) -> None:

pandas/tests/scalar/test_nat.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,13 @@ def test_nat_vector_field_access():
109109
"value", [None, np.nan, iNaT, float("nan"), NaT, "NaT", "nat", "", "NAT"]
110110
)
111111
def test_identity(klass, value):
112-
assert klass(value) is NaT
112+
if value == "" and klass == Timestamp:
113+
msg = "Passing an empty string to Timestamp"
114+
with tm.assert_produces_warning(FutureWarning, match=msg):
115+
result = klass(value)
116+
else:
117+
result = klass(value)
118+
assert result is NaT
113119

114120

115121
@pytest.mark.parametrize("klass", [Timestamp, Timedelta])

pandas/tests/scalar/timestamp/test_constructors.py

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ def test_constructor_float_not_round_with_YM_unit_raises(self):
6262
with pytest.raises(ValueError, match=msg):
6363
Timestamp(150.5, unit="M")
6464

65+
def test_constructor_with_empty_string(self):
66+
msg = "Passing an empty string to Timestamp"
67+
with tm.assert_produces_warning(FutureWarning, match=msg):
68+
Timestamp("")
69+
6570
@pytest.mark.parametrize(
6671
"value, check_kwargs",
6772
[

0 commit comments

Comments
 (0)