Skip to content

BUG: Added deprecation warning to Timestamp constructor #61149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
@@ -446,6 +446,7 @@ Other Deprecations
- Deprecated allowing non-keyword arguments in :meth:`Series.to_string` except ``buf``. (:issue:`57280`)
- 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`)
- 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`)
- Deprecated empty string in :class:`Timestamp` (:issue:`61149`)
- 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`)
- 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`)
- Deprecated parameter ``method`` in :meth:`DataFrame.reindex_like` / :meth:`Series.reindex_like` (:issue:`58667`)
7 changes: 7 additions & 0 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
@@ -2657,6 +2657,13 @@ class Timestamp(_Timestamp):
tzinfo is None):
return ts_input
elif isinstance(ts_input, str):
if ts_input == "":
warnings.warn(
"Passing an empty string to Timestamp is deprecated and will raise "
"a ValueError in a future version. Use `pd.NaT` directly instead.",
FutureWarning,
stacklevel = find_stack_level()
)
# User passed a date string to parse.
# Check that the user didn't also pass a date attribute kwarg.
if any(arg is not None for arg in _date_attributes):
2 changes: 2 additions & 0 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
@@ -547,6 +547,8 @@ def _unbox_scalar(self, value) -> np.datetime64:
return value.as_unit(self.unit, round_ok=False).asm8

def _scalar_from_string(self, value) -> Timestamp | NaTType:
if value == "":
value = NaT
return Timestamp(value, tz=self.tz)

def _check_compatible_with(self, other) -> None:
8 changes: 7 additions & 1 deletion pandas/tests/scalar/test_nat.py
Original file line number Diff line number Diff line change
@@ -109,7 +109,13 @@ def test_nat_vector_field_access():
"value", [None, np.nan, iNaT, float("nan"), NaT, "NaT", "nat", "", "NAT"]
)
def test_identity(klass, value):
assert klass(value) is NaT
if value == "" and klass == Timestamp:
msg = "Passing an empty string to Timestamp"
with tm.assert_produces_warning(FutureWarning, match=msg):
result = klass(value)
else:
result = klass(value)
assert result is NaT


@pytest.mark.parametrize("klass", [Timestamp, Timedelta])
5 changes: 5 additions & 0 deletions pandas/tests/scalar/timestamp/test_constructors.py
Original file line number Diff line number Diff line change
@@ -62,6 +62,11 @@ def test_constructor_float_not_round_with_YM_unit_raises(self):
with pytest.raises(ValueError, match=msg):
Timestamp(150.5, unit="M")

def test_constructor_with_empty_string(self):
msg = "Passing an empty string to Timestamp"
with tm.assert_produces_warning(FutureWarning, match=msg):
Timestamp("")

@pytest.mark.parametrize(
"value, check_kwargs",
[