Skip to content

modified changes for datetime and NaT Objects Behavior closes #43280 #43289

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
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ Datetimelike
^^^^^^^^^^^^
- Bug in :class:`DataFrame` constructor unnecessarily copying non-datetimelike 2D object arrays (:issue:`39272`)
- :func:`to_datetime` would silently swap ``MM/DD/YYYY`` and ``DD/MM/YYYY`` formats if the given ``dayfirst`` option could not be respected - now, a warning is raised in the case of delimited date strings (e.g. ``31-12-2012``) (:issue:`12585`)
-
-Bug in :class:'Series and scalar' to_numeric as inconsistent behaviour for datatime object (:issue:'43280')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Timedelta
^^^^^^^^^
Expand Down
10 changes: 9 additions & 1 deletion pandas/core/tools/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ def to_numeric(arg, errors="raise", downcast=None):
return float(arg)
if is_number(arg):
return arg
if isinstance(arg , datetime):
if pd.isnull(arg):
return np.NaN
else:
return int(pd.to_datetime(arg).strftime("%Y%m%d"))
is_scalars = True
values = np.array([arg], dtype="O")
elif getattr(arg, "ndim", 1) > 1:
Expand All @@ -175,7 +180,10 @@ def to_numeric(arg, errors="raise", downcast=None):
if is_numeric_dtype(values_dtype):
pass
elif is_datetime_or_timedelta_dtype(values_dtype):
values = values.view(np.int64)
if pd.isnull(arg.values):
values = np.nan
else:
values = pd.to_datetime(values).strftime("%Y%m%d").astype(np.int64)
else:
values = ensure_object(values)
coerce_numeric = errors not in ("ignore", "raise")
Expand Down
25 changes: 25 additions & 0 deletions pandas/tests/tools/test_to_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,3 +789,28 @@ def test_to_numeric_scientific_notation():
result = to_numeric("1.7e+308")
expected = np.float64(1.7e308)
assert result == expected


def test_to_numeric_series_date():
#GH 43280
result = pd.to_numeric(pd.Series(datetime(2021, 8, 22)), errors="coerce")
expected = pd.Series([20210822])
tm.assert_series_equal(result, expected)

def test_to_numeric_series_nat():
#GH 43280
result = pd.to_numeric(pd.Series(pd.NaT), errors="coerce")
expected = pd.Series([np.nan])
tm.assert_series_equal(result, expected)

def test_to_numeric_scalar_date():
#GH 43280
result = pd.to_numeric(datetime(2021, 8, 22), errors="coerce")
expected = np.int64(20210822)
assert result == expected

def test_to_numeric_scalar_nat():
#GH 43280
result = pd.to_numeric(pd.NaT, errors="coerce")
assert pd.isnull(result)