From 9edac8ccd593ac659b0407649c03e4ef933bd693 Mon Sep 17 00:00:00 2001 From: th3nn3ss Date: Tue, 23 Aug 2022 15:25:36 +0100 Subject: [PATCH 1/3] TST GH34290 Test for dt.total_seconds() stores float with appending 0.00000000000001 fix --- pandas/tests/arrays/test_timedeltas.py | 29 +++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/pandas/tests/arrays/test_timedeltas.py b/pandas/tests/arrays/test_timedeltas.py index b3b79bd988ad8..ae893a8eedeb4 100644 --- a/pandas/tests/arrays/test_timedeltas.py +++ b/pandas/tests/arrays/test_timedeltas.py @@ -1,4 +1,7 @@ -from datetime import timedelta +from datetime import ( + datetime, + timedelta, +) import numpy as np import pytest @@ -67,6 +70,30 @@ def test_total_seconds(self, unit, tda): expected = tda_nano.total_seconds() tm.assert_numpy_array_equal(result, expected) + def test_series_total_seconds(self): + # GH34290 + expected = float(15) + out = pd.Series( + [pd.Timestamp("2022-03-16 08:32:26"), pd.Timestamp("2022-03-16 08:32:41")] + ) + + result = (out - out.iloc[0]).dt.total_seconds()[1] + assert result == expected + + def test_dateframe_total_seconds(self): + # GH34290 + expected = float(120) + data = {"start": datetime(2020, 1, 1, 12), "end": datetime(2020, 1, 1, 12, 2)} + df = pd.DataFrame(data, index=[0]) + + # try to parse to pd.to_datetime + df["end"] = pd.to_datetime(df["end"]) + df["start"] = pd.to_datetime(df["start"]) + + result = (df["end"] - df["start"]).dt.total_seconds().values[0] + + assert result == expected + @pytest.mark.parametrize( "nat", [np.datetime64("NaT", "ns"), np.datetime64("NaT", "us")] ) From bff99a83ea1b0bd59e6d3acae987f03ffd360097 Mon Sep 17 00:00:00 2001 From: th3nn3ss Date: Wed, 24 Aug 2022 00:52:31 +0100 Subject: [PATCH 2/3] simpler test --- pandas/tests/arrays/test_timedeltas.py | 28 ++++---------------------- 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/pandas/tests/arrays/test_timedeltas.py b/pandas/tests/arrays/test_timedeltas.py index ae893a8eedeb4..c180aa00ffa25 100644 --- a/pandas/tests/arrays/test_timedeltas.py +++ b/pandas/tests/arrays/test_timedeltas.py @@ -1,7 +1,4 @@ -from datetime import ( - datetime, - timedelta, -) +from datetime import timedelta import numpy as np import pytest @@ -70,28 +67,11 @@ def test_total_seconds(self, unit, tda): expected = tda_nano.total_seconds() tm.assert_numpy_array_equal(result, expected) - def test_series_total_seconds(self): - # GH34290 - expected = float(15) - out = pd.Series( - [pd.Timestamp("2022-03-16 08:32:26"), pd.Timestamp("2022-03-16 08:32:41")] - ) - - result = (out - out.iloc[0]).dt.total_seconds()[1] - assert result == expected - - def test_dateframe_total_seconds(self): + def test_timedelta_array_total_seconds(self): # GH34290 - expected = float(120) - data = {"start": datetime(2020, 1, 1, 12), "end": datetime(2020, 1, 1, 12, 2)} - df = pd.DataFrame(data, index=[0]) - - # try to parse to pd.to_datetime - df["end"] = pd.to_datetime(df["end"]) - df["start"] = pd.to_datetime(df["start"]) - - result = (df["end"] - df["start"]).dt.total_seconds().values[0] + expected = pd.Timedelta("2 min").total_seconds() + result = pd.array([pd.Timedelta("2 min")]).total_seconds()[0] assert result == expected @pytest.mark.parametrize( From 201cbf6bc1e9f2a4cdd6ae19b2a42fa10effb0c8 Mon Sep 17 00:00:00 2001 From: th3nn3ss Date: Wed, 24 Aug 2022 01:05:58 +0100 Subject: [PATCH 3/3] use Timedelta instead of pd.Timedelta --- pandas/tests/arrays/test_timedeltas.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/arrays/test_timedeltas.py b/pandas/tests/arrays/test_timedeltas.py index c180aa00ffa25..4ef5ded066790 100644 --- a/pandas/tests/arrays/test_timedeltas.py +++ b/pandas/tests/arrays/test_timedeltas.py @@ -69,9 +69,9 @@ def test_total_seconds(self, unit, tda): def test_timedelta_array_total_seconds(self): # GH34290 - expected = pd.Timedelta("2 min").total_seconds() + expected = Timedelta("2 min").total_seconds() - result = pd.array([pd.Timedelta("2 min")]).total_seconds()[0] + result = pd.array([Timedelta("2 min")]).total_seconds()[0] assert result == expected @pytest.mark.parametrize(