From 5349052667163ea2baa8897b050950c9215bf434 Mon Sep 17 00:00:00 2001 From: Erik Oveson Date: Sat, 17 Nov 2018 20:17:23 -0800 Subject: [PATCH 1/8] TST: Add test case for GH14080 for overflow exception --- .../tests/scalar/timestamp/test_arithmetic.py | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/pandas/tests/scalar/timestamp/test_arithmetic.py b/pandas/tests/scalar/timestamp/test_arithmetic.py index 0f8ddc53734f6..6120d024cfd69 100644 --- a/pandas/tests/scalar/timestamp/test_arithmetic.py +++ b/pandas/tests/scalar/timestamp/test_arithmetic.py @@ -7,11 +7,12 @@ import pandas.util.testing as tm from pandas.compat import long from pandas.tseries import offsets +from pandas.tseries.frequencies import to_offset from pandas import Timestamp, Timedelta class TestTimestampArithmetic(object): - def test_overflow_offset(self): + def test_overflow_offset1(self): # xref https://github.com/statsmodels/statsmodels/issues/3374 # ends up multiplying really large numbers which overflow @@ -27,6 +28,30 @@ def test_overflow_offset(self): with pytest.raises(OverflowError): stamp - offset + def test_overflow_offset2(self): + # xref https://github.com/pandas-dev/pandas/issues/14080 + + stamp = Timestamp("2000/1/1") + offset_overflow = to_offset("D")*100**25 + offset_no_overflow = to_offset("D")*100 + + with pytest.raises(OverflowError): + stamp + offset_overflow + + with pytest.raises(OverflowError): + offset_overflow + stamp + + with pytest.raises(OverflowError): + stamp - offset_overflow + + expected = Timestamp("2000/04/10") + assert stamp + offset_no_overflow == expected + + assert offset_no_overflow + stamp == expected + + expected = Timestamp("1999/09/23") + assert stamp - offset_no_overflow == expected + def test_delta_preserve_nanos(self): val = Timestamp(long(1337299200000000123)) result = val + timedelta(1) From a7df6eade76f0f190332696ae1d9871fd17389aa Mon Sep 17 00:00:00 2001 From: Erik Oveson Date: Sat, 17 Nov 2018 20:39:37 -0800 Subject: [PATCH 2/8] TST: Add test case for GH14080 for overflow exception --- pandas/tests/scalar/timestamp/test_arithmetic.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/tests/scalar/timestamp/test_arithmetic.py b/pandas/tests/scalar/timestamp/test_arithmetic.py index 6120d024cfd69..e8f4d93613f76 100644 --- a/pandas/tests/scalar/timestamp/test_arithmetic.py +++ b/pandas/tests/scalar/timestamp/test_arithmetic.py @@ -30,11 +30,13 @@ def test_overflow_offset1(self): def test_overflow_offset2(self): # xref https://github.com/pandas-dev/pandas/issues/14080 + # used to crash, so check for proper overflow exception stamp = Timestamp("2000/1/1") offset_overflow = to_offset("D")*100**25 offset_no_overflow = to_offset("D")*100 + # overflow expected with pytest.raises(OverflowError): stamp + offset_overflow @@ -44,6 +46,7 @@ def test_overflow_offset2(self): with pytest.raises(OverflowError): stamp - offset_overflow + # no overflow expected expected = Timestamp("2000/04/10") assert stamp + offset_no_overflow == expected From f7be8f3828f9eddd204af577216e388d7cf5da12 Mon Sep 17 00:00:00 2001 From: Erik Oveson Date: Sat, 17 Nov 2018 20:53:20 -0800 Subject: [PATCH 3/8] TST: Add test case for GH14080 for overflow exception --- pandas/tests/scalar/timestamp/test_arithmetic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/scalar/timestamp/test_arithmetic.py b/pandas/tests/scalar/timestamp/test_arithmetic.py index e8f4d93613f76..a79b39b641064 100644 --- a/pandas/tests/scalar/timestamp/test_arithmetic.py +++ b/pandas/tests/scalar/timestamp/test_arithmetic.py @@ -33,8 +33,8 @@ def test_overflow_offset2(self): # used to crash, so check for proper overflow exception stamp = Timestamp("2000/1/1") - offset_overflow = to_offset("D")*100**25 - offset_no_overflow = to_offset("D")*100 + offset_overflow = to_offset("D") * 100 ** 25 + offset_no_overflow = to_offset("D") * 100 # overflow expected with pytest.raises(OverflowError): From c727003fd4f9619dffe3c555de8cce3158497919 Mon Sep 17 00:00:00 2001 From: Erik Oveson Date: Sun, 18 Nov 2018 08:45:33 -0800 Subject: [PATCH 4/8] TST: For GH14080, break up tests, test message --- .../tests/scalar/timestamp/test_arithmetic.py | 51 ++++++++++--------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/pandas/tests/scalar/timestamp/test_arithmetic.py b/pandas/tests/scalar/timestamp/test_arithmetic.py index a79b39b641064..207bd103105ea 100644 --- a/pandas/tests/scalar/timestamp/test_arithmetic.py +++ b/pandas/tests/scalar/timestamp/test_arithmetic.py @@ -12,49 +12,54 @@ class TestTimestampArithmetic(object): - def test_overflow_offset1(self): + def test_overflow_offset(self): + # no overflow expected + + stamp = Timestamp("2000/1/1") + offset_no_overflow = to_offset("D") * 100 + + expected = Timestamp("2000/04/10") + assert stamp + offset_no_overflow == expected + + assert offset_no_overflow + stamp == expected + + expected = Timestamp("1999/09/23") + assert stamp - offset_no_overflow == expected + + def test_overflow_offset_raises(self): # xref https://github.com/statsmodels/statsmodels/issues/3374 # ends up multiplying really large numbers which overflow stamp = Timestamp('2017-01-13 00:00:00', freq='D') - offset = 20169940 * offsets.Day(1) + offset_overflow = 20169940 * offsets.Day(1) + msg = ("the add operation between " + r"\<-?\d+ \* Days\> and \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} " + "will overflow") - with pytest.raises(OverflowError): - stamp + offset + with pytest.raises(OverflowError, match=msg): + stamp + offset_overflow - with pytest.raises(OverflowError): - offset + stamp + with pytest.raises(OverflowError, match=msg): + offset_overflow + stamp - with pytest.raises(OverflowError): - stamp - offset + with pytest.raises(OverflowError, match=msg): + stamp - offset_overflow - def test_overflow_offset2(self): # xref https://github.com/pandas-dev/pandas/issues/14080 # used to crash, so check for proper overflow exception stamp = Timestamp("2000/1/1") offset_overflow = to_offset("D") * 100 ** 25 - offset_no_overflow = to_offset("D") * 100 - # overflow expected - with pytest.raises(OverflowError): + with pytest.raises(OverflowError, match=msg): stamp + offset_overflow - with pytest.raises(OverflowError): + with pytest.raises(OverflowError, match=msg): offset_overflow + stamp - with pytest.raises(OverflowError): + with pytest.raises(OverflowError, match=msg): stamp - offset_overflow - # no overflow expected - expected = Timestamp("2000/04/10") - assert stamp + offset_no_overflow == expected - - assert offset_no_overflow + stamp == expected - - expected = Timestamp("1999/09/23") - assert stamp - offset_no_overflow == expected - def test_delta_preserve_nanos(self): val = Timestamp(long(1337299200000000123)) result = val + timedelta(1) From eecede19e4ac4afc97802f2496e7fe98343b8039 Mon Sep 17 00:00:00 2001 From: Erik Oveson Date: Sun, 18 Nov 2018 22:33:28 -0800 Subject: [PATCH 5/8] TST: For GH4861, Period and datetime in multiindex --- pandas/tests/indexes/period/test_period.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pandas/tests/indexes/period/test_period.py b/pandas/tests/indexes/period/test_period.py index ddb3fe686534a..4a18ce425be66 100644 --- a/pandas/tests/indexes/period/test_period.py +++ b/pandas/tests/indexes/period/test_period.py @@ -1,3 +1,5 @@ +from datetime import datetime + import numpy as np import pytest @@ -570,3 +572,20 @@ def test_maybe_convert_timedelta(): offset = offsets.BusinessDay() with pytest.raises(ValueError, match='freq'): pi._maybe_convert_timedelta(offset) + + +def test_multiindex_period_datetime(): + # GH4861, using datetime in period of multiindex raises exception + + idx1 = Index(['a', 'a', 'a', 'b', 'b']) + idx2 = period_range('2012-01', periods=len(idx1), freq='M') + s = Series(np.random.randn(len(idx1)), [idx1, idx2]) + + # try Period as index + expected = s.iloc[0] + result = s.loc['a', Period('2012-01')] + assert result == expected + + # try datetime as index + result = s.loc['a', datetime(2012, 1, 1)] + assert result == expected From ae90f93757898814ded05f78549123e2e94ce834 Mon Sep 17 00:00:00 2001 From: Erik Oveson Date: Mon, 19 Nov 2018 07:19:44 -0800 Subject: [PATCH 6/8] TST: GH4861 move changes to different test file --- pandas/tests/indexes/period/test_period.py | 19 ------------------- pandas/tests/indexing/test_multiindex.py | 22 +++++++++++++++++++++- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/pandas/tests/indexes/period/test_period.py b/pandas/tests/indexes/period/test_period.py index 4a18ce425be66..ddb3fe686534a 100644 --- a/pandas/tests/indexes/period/test_period.py +++ b/pandas/tests/indexes/period/test_period.py @@ -1,5 +1,3 @@ -from datetime import datetime - import numpy as np import pytest @@ -572,20 +570,3 @@ def test_maybe_convert_timedelta(): offset = offsets.BusinessDay() with pytest.raises(ValueError, match='freq'): pi._maybe_convert_timedelta(offset) - - -def test_multiindex_period_datetime(): - # GH4861, using datetime in period of multiindex raises exception - - idx1 = Index(['a', 'a', 'a', 'b', 'b']) - idx2 = period_range('2012-01', periods=len(idx1), freq='M') - s = Series(np.random.randn(len(idx1)), [idx1, idx2]) - - # try Period as index - expected = s.iloc[0] - result = s.loc['a', Period('2012-01')] - assert result == expected - - # try datetime as index - result = s.loc['a', datetime(2012, 1, 1)] - assert result == expected diff --git a/pandas/tests/indexing/test_multiindex.py b/pandas/tests/indexing/test_multiindex.py index ea17844a75033..17fb9952c8680 100644 --- a/pandas/tests/indexing/test_multiindex.py +++ b/pandas/tests/indexing/test_multiindex.py @@ -1,5 +1,7 @@ from warnings import catch_warnings +from datetime import datetime + import numpy as np import pytest @@ -7,7 +9,8 @@ import pandas as pd from pandas import ( - DataFrame, Index, MultiIndex, Panel, Series, Timestamp, date_range) + DataFrame, date_range, Index, MultiIndex, Panel, Period, period_range, + Series, Timestamp) from pandas.tests.indexing.common import _mklbl from pandas.util import testing as tm @@ -1340,3 +1343,20 @@ def test_panel_setitem_with_multiindex(self): p5.iloc[0, :, 0] = [1, 2] expected = Panel(arr, **axes) tm.assert_panel_equal(p5, expected) + + +def test_multiindex_period_datetime(): + # GH4861, using datetime in period of multiindex raises exception + + idx1 = Index(['a', 'a', 'a', 'b', 'b']) + idx2 = period_range('2012-01', periods=len(idx1), freq='M') + s = Series(np.random.randn(len(idx1)), [idx1, idx2]) + + # try Period as index + expected = s.iloc[0] + result = s.loc['a', Period('2012-01')] + assert result == expected + + # try datetime as index + result = s.loc['a', datetime(2012, 1, 1)] + assert result == expected From 4f16c8b9d9af83880144fc5d3046e20519ae554c Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Mon, 19 Nov 2018 20:58:40 -0500 Subject: [PATCH 7/8] fix sorting --- pandas/tests/indexing/test_multiindex.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pandas/tests/indexing/test_multiindex.py b/pandas/tests/indexing/test_multiindex.py index 17fb9952c8680..f4caf17b60d65 100644 --- a/pandas/tests/indexing/test_multiindex.py +++ b/pandas/tests/indexing/test_multiindex.py @@ -1,6 +1,5 @@ -from warnings import catch_warnings - from datetime import datetime +from warnings import catch_warnings import numpy as np import pytest @@ -9,8 +8,8 @@ import pandas as pd from pandas import ( - DataFrame, date_range, Index, MultiIndex, Panel, Period, period_range, - Series, Timestamp) + DataFrame, Index, MultiIndex, Panel, Period, Series, Timestamp, date_range, + period_range) from pandas.tests.indexing.common import _mklbl from pandas.util import testing as tm From 8d0bb3770fe9971ce7a9c4d2eaac55ba8ac3e58a Mon Sep 17 00:00:00 2001 From: Erik Oveson Date: Mon, 19 Nov 2018 21:29:35 -0800 Subject: [PATCH 8/8] TST: Add tests for GH6173, appends to empty df --- pandas/tests/indexing/test_loc.py | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index df0180c7a5bf7..21bb624790328 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -804,3 +804,35 @@ def test_loc_uint64(self): result = s.loc[[np.iinfo('uint64').max - 1, np.iinfo('uint64').max]] tm.assert_series_equal(result, s) + + def test_loc_setitem_empty_append(self): + # GH6173, various appends to an empty dataframe + + data = [1, 2, 3] + expected = DataFrame({'x': data, 'y': [None] * len(data)}) + + # appends to fit length of data + df = DataFrame(columns=['x', 'y']) + df.loc[:, 'x'] = data + tm.assert_frame_equal(df, expected) + + # only appends one value + expected = DataFrame({'x': [1.0], 'y': [np.nan]}) + df = DataFrame(columns=['x', 'y'], + dtype=np.float) + df.loc[0, 'x'] = expected.loc[0, 'x'] + tm.assert_frame_equal(df, expected) + + def test_loc_setitem_empty_append_raises(self): + # GH6173, various appends to an empty dataframe + + data = [1, 2] + df = DataFrame(columns=['x', 'y']) + msg = (r"None of \[Int64Index\(\[0, 1\], dtype='int64'\)\] " + r"are in the \[index\]") + with pytest.raises(KeyError, match=msg): + df.loc[[0, 1], 'x'] = data + + msg = "cannot copy sequence with size 2 to array axis with dimension 0" + with pytest.raises(ValueError, match=msg): + df.loc[0:2, 'x'] = data