From 5d554123079a2a1c868d6585d0e923702267576c Mon Sep 17 00:00:00 2001 From: deepang17 <47976918+deepang17@users.noreply.github.com> Date: Sun, 7 Mar 2021 12:22:03 +0530 Subject: [PATCH 01/14] STYLE: Inconsistent namespace - tools (pandas-dev#39992) --- pandas/tests/tools/test_to_datetime.py | 270 ++++++++++++------------ pandas/tests/tools/test_to_numeric.py | 10 +- pandas/tests/tools/test_to_timedelta.py | 18 +- 3 files changed, 149 insertions(+), 149 deletions(-) diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index 1a47b5b37e3d2..cda056e7f4c96 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -111,14 +111,14 @@ def test_to_datetime_format_YYYYMMDD(self, cache): # coercion # GH 7930 s = Series([20121231, 20141231, 99991231]) - result = pd.to_datetime(s, format="%Y%m%d", errors="ignore", cache=cache) + result = to_datetime(s, format="%Y%m%d", errors="ignore", cache=cache) expected = Series( [datetime(2012, 12, 31), datetime(2014, 12, 31), datetime(9999, 12, 31)], dtype=object, ) tm.assert_series_equal(result, expected) - result = pd.to_datetime(s, format="%Y%m%d", errors="coerce", cache=cache) + result = to_datetime(s, format="%Y%m%d", errors="coerce", cache=cache) expected = Series(["20121231", "20141231", "NaT"], dtype="M8[ns]") tm.assert_series_equal(result, expected) @@ -128,12 +128,12 @@ def test_to_datetime_format_YYYYMMDD(self, cache): # Null values with Strings ["19801222", "20010112", None], ["19801222", "20010112", np.nan], - ["19801222", "20010112", pd.NaT], + ["19801222", "20010112", NaT], ["19801222", "20010112", "NaT"], # Null values with Integers [19801222, 20010112, None], [19801222, 20010112, np.nan], - [19801222, 20010112, pd.NaT], + [19801222, 20010112, NaT], [19801222, 20010112, "NaT"], ], ) @@ -141,8 +141,8 @@ def test_to_datetime_format_YYYYMMDD_with_none(self, input_s): # GH 30011 # format='%Y%m%d' # with None - expected = Series([Timestamp("19801222"), Timestamp("20010112"), pd.NaT]) - result = Series(pd.to_datetime(input_s, format="%Y%m%d")) + expected = Series([Timestamp("19801222"), Timestamp("20010112"), NaT]) + result = Series(to_datetime(input_s, format="%Y%m%d")) tm.assert_series_equal(result, expected) @pytest.mark.parametrize( @@ -173,7 +173,7 @@ def test_to_datetime_format_YYYYMMDD_with_none(self, input_s): def test_to_datetime_format_YYYYMMDD_overflow(self, input_s, expected): # GH 25512 # format='%Y%m%d', errors='coerce' - result = pd.to_datetime(input_s, format="%Y%m%d", errors="coerce") + result = to_datetime(input_s, format="%Y%m%d", errors="coerce") tm.assert_series_equal(result, expected) @pytest.mark.parametrize("cache", [True, False]) @@ -273,8 +273,8 @@ def test_parse_nanoseconds_with_formula(self, cache): "2012-01-01 09:00:00.001000", "2012-01-01 09:00:00.001000000", ]: - expected = pd.to_datetime(v, cache=cache) - result = pd.to_datetime(v, format="%Y-%m-%d %H:%M:%S.%f", cache=cache) + expected = to_datetime(v, cache=cache) + result = to_datetime(v, format="%Y-%m-%d %H:%M:%S.%f", cache=cache) assert result == expected @pytest.mark.parametrize("cache", [True, False]) @@ -339,7 +339,7 @@ def test_to_datetime_format_weeks(self, cache): ) def test_to_datetime_parse_tzname_or_tzoffset(self, fmt, dates, expected_dates): # GH 13486 - result = pd.to_datetime(dates, format=fmt) + result = to_datetime(dates, format=fmt) expected = Index(expected_dates) tm.assert_equal(result, expected) @@ -359,7 +359,7 @@ def test_to_datetime_parse_tzname_or_tzoffset_different_tz_to_utc(self): ] fmt = "%Y-%m-%d %H:%M:%S %z" - result = pd.to_datetime(dates, format=fmt, utc=True) + result = to_datetime(dates, format=fmt, utc=True) expected = DatetimeIndex(expected_dates) tm.assert_index_equal(result, expected) @@ -372,13 +372,13 @@ def test_to_datetime_parse_timezone_malformed(self, offset): msg = "does not match format|unconverted data remains" with pytest.raises(ValueError, match=msg): - pd.to_datetime([date], format=fmt) + to_datetime([date], format=fmt) def test_to_datetime_parse_timezone_keeps_name(self): # GH 21697 fmt = "%Y-%m-%d %H:%M:%S %z" arg = Index(["2010-01-01 12:00:00 Z"], name="foo") - result = pd.to_datetime(arg, format=fmt) + result = to_datetime(arg, format=fmt) expected = DatetimeIndex(["2010-01-01 12:00:00"], tz="UTC", name="foo") tm.assert_index_equal(result, expected) @@ -507,25 +507,25 @@ def test_to_datetime_dtarr(self, tz): assert result is arr def test_to_datetime_pydatetime(self): - actual = pd.to_datetime(datetime(2008, 1, 15)) + actual = to_datetime(datetime(2008, 1, 15)) assert actual == datetime(2008, 1, 15) def test_to_datetime_YYYYMMDD(self): - actual = pd.to_datetime("20080115") + actual = to_datetime("20080115") assert actual == datetime(2008, 1, 15) def test_to_datetime_unparseable_ignore(self): # unparseable s = "Month 1, 1999" - assert pd.to_datetime(s, errors="ignore") == s + assert to_datetime(s, errors="ignore") == s @td.skip_if_windows # `tm.set_timezone` does not work in windows def test_to_datetime_now(self): # See GH#18666 with tm.set_timezone("US/Eastern"): npnow = np.datetime64("now").astype("datetime64[ns]") - pdnow = pd.to_datetime("now") - pdnow2 = pd.to_datetime(["now"])[0] + pdnow = to_datetime("now") + pdnow2 = to_datetime(["now"])[0] # These should all be equal with infinite perf; this gives # a generous margin of 10 seconds @@ -545,8 +545,8 @@ def test_to_datetime_today(self): # so this test will not detect the regression introduced in #18666. with tm.set_timezone("Pacific/Auckland"): # 12-13 hours ahead of UTC nptoday = np.datetime64("today").astype("datetime64[ns]").astype(np.int64) - pdtoday = pd.to_datetime("today") - pdtoday2 = pd.to_datetime(["today"])[0] + pdtoday = to_datetime("today") + pdtoday2 = to_datetime(["today"])[0] tstoday = Timestamp("today") tstoday2 = Timestamp.today() @@ -563,8 +563,8 @@ def test_to_datetime_today(self): with tm.set_timezone("US/Samoa"): # 11 hours behind UTC nptoday = np.datetime64("today").astype("datetime64[ns]").astype(np.int64) - pdtoday = pd.to_datetime("today") - pdtoday2 = pd.to_datetime(["today"])[0] + pdtoday = to_datetime("today") + pdtoday2 = to_datetime(["today"])[0] # These should all be equal with infinite perf; this gives # a generous margin of 10 seconds @@ -583,7 +583,7 @@ def test_to_datetime_dt64s(self, cache): in_bound_dts = [np.datetime64("2000-01-01"), np.datetime64("2000-01-02")] for dt in in_bound_dts: - assert pd.to_datetime(dt, cache=cache) == Timestamp(dt) + assert to_datetime(dt, cache=cache) == Timestamp(dt) @pytest.mark.parametrize( "dt", [np.datetime64("1000-01-01"), np.datetime64("5000-01-02")] @@ -592,10 +592,10 @@ def test_to_datetime_dt64s(self, cache): def test_to_datetime_dt64s_out_of_bounds(self, cache, dt): msg = f"Out of bounds nanosecond timestamp: {dt}" with pytest.raises(OutOfBoundsDatetime, match=msg): - pd.to_datetime(dt, errors="raise") + to_datetime(dt, errors="raise") with pytest.raises(OutOfBoundsDatetime, match=msg): Timestamp(dt) - assert pd.to_datetime(dt, errors="coerce", cache=cache) is NaT + assert to_datetime(dt, errors="coerce", cache=cache) is NaT @pytest.mark.parametrize("cache", [True, False]) @pytest.mark.parametrize("unit", ["s", "D"]) @@ -609,7 +609,7 @@ def test_to_datetime_array_of_dt64s(self, cache, unit): # Assuming all datetimes are in bounds, to_datetime() returns # an array that is equal to Timestamp() parsing tm.assert_index_equal( - pd.to_datetime(dts, cache=cache), + to_datetime(dts, cache=cache), DatetimeIndex([Timestamp(x).asm8 for x in dts]), ) @@ -618,13 +618,13 @@ def test_to_datetime_array_of_dt64s(self, cache, unit): msg = "Out of bounds nanosecond timestamp: 9999-01-01 00:00:00" with pytest.raises(OutOfBoundsDatetime, match=msg): - pd.to_datetime(dts_with_oob, errors="raise") + to_datetime(dts_with_oob, errors="raise") tm.assert_index_equal( - pd.to_datetime(dts_with_oob, errors="coerce", cache=cache), + to_datetime(dts_with_oob, errors="coerce", cache=cache), DatetimeIndex( [Timestamp(dts_with_oob[0]).asm8, Timestamp(dts_with_oob[1]).asm8] * 30 - + [pd.NaT], + + [NaT], ), ) @@ -632,7 +632,7 @@ def test_to_datetime_array_of_dt64s(self, cache, unit): # are converted to their .item(), which depending on the version of # numpy is either a python datetime.datetime or datetime.date tm.assert_index_equal( - pd.to_datetime(dts_with_oob, errors="ignore", cache=cache), + to_datetime(dts_with_oob, errors="ignore", cache=cache), Index([dt.item() for dt in dts_with_oob]), ) @@ -645,7 +645,7 @@ def test_to_datetime_tz(self, cache): Timestamp("2013-01-01 13:00:00-0800", tz="US/Pacific"), Timestamp("2013-01-02 14:00:00-0800", tz="US/Pacific"), ] - result = pd.to_datetime(arr, cache=cache) + result = to_datetime(arr, cache=cache) expected = DatetimeIndex( ["2013-01-01 13:00:00", "2013-01-02 14:00:00"], tz="US/Pacific" ) @@ -661,7 +661,7 @@ def test_to_datetime_tz(self, cache): "converted to datetime64 unless utc=True" ) with pytest.raises(ValueError, match=msg): - pd.to_datetime(arr, cache=cache) + to_datetime(arr, cache=cache) @pytest.mark.parametrize("cache", [True, False]) def test_to_datetime_different_offsets(self, cache): @@ -671,7 +671,7 @@ def test_to_datetime_different_offsets(self, cache): ts_string_2 = "March 1, 2018 12:00:00+0500" arr = [ts_string_1] * 5 + [ts_string_2] * 5 expected = Index([parse(x) for x in arr]) - result = pd.to_datetime(arr, cache=cache) + result = to_datetime(arr, cache=cache) tm.assert_index_equal(result, expected) @pytest.mark.parametrize("cache", [True, False]) @@ -689,7 +689,7 @@ def test_to_datetime_tz_pytz(self, cache): ], dtype=object, ) - result = pd.to_datetime(arr, utc=True, cache=cache) + result = to_datetime(arr, utc=True, cache=cache) expected = DatetimeIndex( ["2000-01-01 08:00:00+00:00", "2000-06-01 07:00:00+00:00"], dtype="datetime64[ns, UTC]", @@ -717,7 +717,7 @@ def test_to_datetime_utc_true( Timestamp("2010-01-02 12:13:15", tz="utc"), ] - result = pd.to_datetime( + result = to_datetime( init_constructor(data), format="%Y%m%d %H%M%S", utc=True, cache=cache ) expected = end_constructor(expected_data) @@ -725,7 +725,7 @@ def test_to_datetime_utc_true( # Test scalar case as well for scalar, expected in zip(data, expected_data): - result = pd.to_datetime( + result = to_datetime( scalar, format="%Y%m%d %H%M%S", utc=True, cache=cache ) assert result == expected @@ -734,7 +734,7 @@ def test_to_datetime_utc_true( def test_to_datetime_utc_true_with_series_single_value(self, cache): # GH 15760 UTC=True with Series ts = 1.5e18 - result = pd.to_datetime(Series([ts]), utc=True, cache=cache) + result = to_datetime(Series([ts]), utc=True, cache=cache) expected = Series([Timestamp(ts, tz="utc")]) tm.assert_series_equal(result, expected) @@ -743,7 +743,7 @@ def test_to_datetime_utc_true_with_series_tzaware_string(self, cache): ts = "2013-01-01 00:00:00-01:00" expected_ts = "2013-01-01 01:00:00" data = Series([ts] * 3) - result = pd.to_datetime(data, utc=True, cache=cache) + result = to_datetime(data, utc=True, cache=cache) expected = Series([Timestamp(expected_ts, tz="utc")] * 3) tm.assert_series_equal(result, expected) @@ -757,7 +757,7 @@ def test_to_datetime_utc_true_with_series_tzaware_string(self, cache): ) def test_to_datetime_utc_true_with_series_datetime_ns(self, cache, date, dtype): expected = Series([Timestamp("2013-01-01 01:00:00", tz="UTC")]) - result = pd.to_datetime(Series([date], dtype=dtype), utc=True, cache=cache) + result = to_datetime(Series([date], dtype=dtype), utc=True, cache=cache) tm.assert_series_equal(result, expected) @pytest.mark.parametrize("cache", [True, False]) @@ -778,7 +778,7 @@ def test_to_datetime_tz_psycopg2(self, cache): dtype=object, ) - result = pd.to_datetime(arr, errors="coerce", utc=True, cache=cache) + result = to_datetime(arr, errors="coerce", utc=True, cache=cache) expected = DatetimeIndex( ["2000-01-01 08:00:00+00:00", "2000-06-01 07:00:00+00:00"], dtype="datetime64[ns, UTC]", @@ -794,10 +794,10 @@ def test_to_datetime_tz_psycopg2(self, cache): assert is_datetime64_ns_dtype(i) # tz coercion - result = pd.to_datetime(i, errors="coerce", cache=cache) + result = to_datetime(i, errors="coerce", cache=cache) tm.assert_index_equal(result, i) - result = pd.to_datetime(i, errors="coerce", utc=True, cache=cache) + result = to_datetime(i, errors="coerce", utc=True, cache=cache) expected = DatetimeIndex(["2000-01-01 13:00:00"], dtype="datetime64[ns, UTC]") tm.assert_index_equal(result, expected) @@ -829,24 +829,24 @@ def test_datetime_invalid_datatype(self): # GH13176 msg = "is not convertible to datetime" with pytest.raises(TypeError, match=msg): - pd.to_datetime(bool) + to_datetime(bool) with pytest.raises(TypeError, match=msg): - pd.to_datetime(pd.to_datetime) + to_datetime(to_datetime) @pytest.mark.parametrize("value", ["a", "00:01:99"]) @pytest.mark.parametrize("infer", [True, False]) @pytest.mark.parametrize("format", [None, "H%:M%:S%"]) def test_datetime_invalid_scalar(self, value, format, infer): # GH24763 - res = pd.to_datetime( + res = to_datetime( value, errors="ignore", format=format, infer_datetime_format=infer ) assert res == value - res = pd.to_datetime( + res = to_datetime( value, errors="coerce", format=format, infer_datetime_format=infer ) - assert res is pd.NaT + assert res is NaT msg = ( "is a bad directive in format|" @@ -854,7 +854,7 @@ def test_datetime_invalid_scalar(self, value, format, infer): "Given date string not likely a datetime" ) with pytest.raises(ValueError, match=msg): - pd.to_datetime( + to_datetime( value, errors="raise", format=format, infer_datetime_format=infer ) @@ -863,26 +863,26 @@ def test_datetime_invalid_scalar(self, value, format, infer): @pytest.mark.parametrize("format", [None, "H%:M%:S%"]) def test_datetime_outofbounds_scalar(self, value, format, infer): # GH24763 - res = pd.to_datetime( + res = to_datetime( value, errors="ignore", format=format, infer_datetime_format=infer ) assert res == value - res = pd.to_datetime( + res = to_datetime( value, errors="coerce", format=format, infer_datetime_format=infer ) - assert res is pd.NaT + assert res is NaT if format is not None: msg = "is a bad directive in format|Out of bounds nanosecond timestamp" with pytest.raises(ValueError, match=msg): - pd.to_datetime( + to_datetime( value, errors="raise", format=format, infer_datetime_format=infer ) else: msg = "Out of bounds nanosecond timestamp" with pytest.raises(OutOfBoundsDatetime, match=msg): - pd.to_datetime( + to_datetime( value, errors="raise", format=format, infer_datetime_format=infer ) @@ -891,15 +891,15 @@ def test_datetime_outofbounds_scalar(self, value, format, infer): @pytest.mark.parametrize("format", [None, "H%:M%:S%"]) def test_datetime_invalid_index(self, values, format, infer): # GH24763 - res = pd.to_datetime( + res = to_datetime( values, errors="ignore", format=format, infer_datetime_format=infer ) tm.assert_index_equal(res, Index(values)) - res = pd.to_datetime( + res = to_datetime( values, errors="coerce", format=format, infer_datetime_format=infer ) - tm.assert_index_equal(res, DatetimeIndex([pd.NaT] * len(values))) + tm.assert_index_equal(res, DatetimeIndex([NaT] * len(values))) msg = ( "is a bad directive in format|" @@ -907,7 +907,7 @@ def test_datetime_invalid_index(self, values, format, infer): "second must be in 0..59" ) with pytest.raises(ValueError, match=msg): - pd.to_datetime( + to_datetime( values, errors="raise", format=format, infer_datetime_format=infer ) @@ -919,8 +919,8 @@ def test_to_datetime_cache(self, utc, format, constructor): test_dates = [date] * 10 ** 5 data = constructor(test_dates) - result = pd.to_datetime(data, utc=utc, format=format, cache=True) - expected = pd.to_datetime(data, utc=utc, format=format, cache=False) + result = to_datetime(data, utc=utc, format=format, cache=True) + expected = to_datetime(data, utc=utc, format=format, cache=False) tm.assert_index_equal(result, expected) @@ -938,8 +938,8 @@ def test_no_slicing_errors_in_should_cache(self, listlike): def test_to_datetime_from_deque(self): # GH 29403 - result = pd.to_datetime(deque([Timestamp("2010-06-02 09:30:00")] * 51)) - expected = pd.to_datetime([Timestamp("2010-06-02 09:30:00")] * 51) + result = to_datetime(deque([Timestamp("2010-06-02 09:30:00")] * 51)) + expected = to_datetime([Timestamp("2010-06-02 09:30:00")] * 51) tm.assert_index_equal(result, expected) @pytest.mark.parametrize("utc", [True, None]) @@ -948,13 +948,13 @@ def test_to_datetime_cache_series(self, utc, format): date = "20130101 00:00:00" test_dates = [date] * 10 ** 5 data = Series(test_dates) - result = pd.to_datetime(data, utc=utc, format=format, cache=True) - expected = pd.to_datetime(data, utc=utc, format=format, cache=False) + result = to_datetime(data, utc=utc, format=format, cache=True) + expected = to_datetime(data, utc=utc, format=format, cache=False) tm.assert_series_equal(result, expected) def test_to_datetime_cache_scalar(self): date = "20130101 00:00:00" - result = pd.to_datetime(date, cache=True) + result = to_datetime(date, cache=True) expected = Timestamp("20130101 00:00:00") assert result == expected @@ -974,7 +974,7 @@ def test_week_without_day_and_calendar_year(self, date, format): msg = "Cannot use '%W' or '%U' without day and year" with pytest.raises(ValueError, match=msg): - pd.to_datetime(date, format=format) + to_datetime(date, format=format) def test_to_datetime_coerce(self): # GH 26122 @@ -1038,7 +1038,7 @@ def test_iso_8601_strings_with_different_offsets(self): def test_iso8601_strings_mixed_offsets_with_naive(self): # GH 24992 - result = pd.to_datetime( + result = to_datetime( [ "2018-11-28T00:00:00", "2018-11-28T00:00:00+12:00", @@ -1048,7 +1048,7 @@ def test_iso8601_strings_mixed_offsets_with_naive(self): ], utc=True, ) - expected = pd.to_datetime( + expected = to_datetime( [ "2018-11-28T00:00:00", "2018-11-27T12:00:00", @@ -1061,8 +1061,8 @@ def test_iso8601_strings_mixed_offsets_with_naive(self): tm.assert_index_equal(result, expected) items = ["2018-11-28T00:00:00+12:00", "2018-11-28T00:00:00"] - result = pd.to_datetime(items, utc=True) - expected = pd.to_datetime(list(reversed(items)), utc=True)[::-1] + result = to_datetime(items, utc=True) + expected = to_datetime(list(reversed(items)), utc=True)[::-1] tm.assert_index_equal(result, expected) def test_mixed_offsets_with_native_datetime_raises(self): @@ -1077,7 +1077,7 @@ def test_mixed_offsets_with_native_datetime_raises(self): ] ) with pytest.raises(ValueError, match="Tz-aware datetime.datetime"): - pd.to_datetime(s) + to_datetime(s) def test_non_iso_strings_with_tz_offset(self): result = to_datetime(["March 1, 2018 12:00:00+0400"] * 2) @@ -1106,7 +1106,7 @@ def test_to_datetime_with_format_out_of_bounds(self, dt_str): # GH 9107 msg = "Out of bounds nanosecond timestamp" with pytest.raises(OutOfBoundsDatetime, match=msg): - pd.to_datetime(dt_str, format="%Y%m%d") + to_datetime(dt_str, format="%Y%m%d") def test_to_datetime_utc(self): arr = np.array([parse("2012-06-13T01:39:00Z")], dtype=object) @@ -1191,15 +1191,15 @@ def test_unit_consistency(self, cache): # consistency of conversions expected = Timestamp("1970-05-09 14:25:11") - result = pd.to_datetime(11111111, unit="s", errors="raise", cache=cache) + result = to_datetime(11111111, unit="s", errors="raise", cache=cache) assert result == expected assert isinstance(result, Timestamp) - result = pd.to_datetime(11111111, unit="s", errors="coerce", cache=cache) + result = to_datetime(11111111, unit="s", errors="coerce", cache=cache) assert result == expected assert isinstance(result, Timestamp) - result = pd.to_datetime(11111111, unit="s", errors="ignore", cache=cache) + result = to_datetime(11111111, unit="s", errors="ignore", cache=cache) assert result == expected assert isinstance(result, Timestamp) @@ -1212,24 +1212,24 @@ def test_unit_with_numeric(self, cache): arr1 = [1.434692e18, 1.432766e18] arr2 = np.array(arr1).astype("int64") for errors in ["ignore", "raise", "coerce"]: - result = pd.to_datetime(arr1, errors=errors, cache=cache) + result = to_datetime(arr1, errors=errors, cache=cache) tm.assert_index_equal(result, expected) - result = pd.to_datetime(arr2, errors=errors, cache=cache) + result = to_datetime(arr2, errors=errors, cache=cache) tm.assert_index_equal(result, expected) # but we want to make sure that we are coercing # if we have ints/strings expected = DatetimeIndex(["NaT", "2015-06-19 05:33:20", "2015-05-27 22:33:20"]) arr = ["foo", 1.434692e18, 1.432766e18] - result = pd.to_datetime(arr, errors="coerce", cache=cache) + result = to_datetime(arr, errors="coerce", cache=cache) tm.assert_index_equal(result, expected) expected = DatetimeIndex( ["2015-06-19 05:33:20", "2015-05-27 22:33:20", "NaT", "NaT"] ) arr = [1.434692e18, 1.432766e18, "foo", "NaT"] - result = pd.to_datetime(arr, errors="coerce", cache=cache) + result = to_datetime(arr, errors="coerce", cache=cache) tm.assert_index_equal(result, expected) @pytest.mark.parametrize("cache", [True, False]) @@ -1238,26 +1238,26 @@ def test_unit_mixed(self, cache): # mixed integers/datetimes expected = DatetimeIndex(["2013-01-01", "NaT", "NaT"]) arr = [Timestamp("20130101"), 1.434692e18, 1.432766e18] - result = pd.to_datetime(arr, errors="coerce", cache=cache) + result = to_datetime(arr, errors="coerce", cache=cache) tm.assert_index_equal(result, expected) msg = "mixed datetimes and integers in passed array" with pytest.raises(ValueError, match=msg): - pd.to_datetime(arr, errors="raise", cache=cache) + to_datetime(arr, errors="raise", cache=cache) expected = DatetimeIndex(["NaT", "NaT", "2013-01-01"]) arr = [1.434692e18, 1.432766e18, Timestamp("20130101")] - result = pd.to_datetime(arr, errors="coerce", cache=cache) + result = to_datetime(arr, errors="coerce", cache=cache) tm.assert_index_equal(result, expected) with pytest.raises(ValueError, match=msg): - pd.to_datetime(arr, errors="raise", cache=cache) + to_datetime(arr, errors="raise", cache=cache) @pytest.mark.parametrize("cache", [True, False]) def test_unit_rounding(self, cache): # GH 14156 & GH 20445: argument will incur floating point errors # but no premature rounding - result = pd.to_datetime(1434743731.8770001, unit="s", cache=cache) + result = to_datetime(1434743731.8770001, unit="s", cache=cache) expected = Timestamp("2015-06-19 19:55:31.877000192") assert result == expected @@ -1265,7 +1265,7 @@ def test_unit_rounding(self, cache): def test_unit_ignore_keeps_name(self, cache): # GH 21697 expected = Index([15e9] * 2, name="name") - result = pd.to_datetime(expected, errors="ignore", unit="s", cache=cache) + result = to_datetime(expected, errors="ignore", unit="s", cache=cache) tm.assert_index_equal(result, expected) @pytest.mark.parametrize("cache", [True, False]) @@ -1437,7 +1437,7 @@ def test_dataframe_dtypes(self, cache): def test_dataframe_utc_true(self): # GH 23760 df = DataFrame({"year": [2015, 2016], "month": [2, 3], "day": [4, 5]}) - result = pd.to_datetime(df, utc=True) + result = to_datetime(df, utc=True) expected = Series( np.array(["2015-02-04", "2016-03-05"], dtype="datetime64[ns]") ).dt.tz_localize("UTC") @@ -1445,7 +1445,7 @@ def test_dataframe_utc_true(self): def test_to_datetime_errors_ignore_utc_true(self): # GH 23758 - result = pd.to_datetime([1], unit="s", utc=True, errors="ignore") + result = to_datetime([1], unit="s", utc=True, errors="ignore") expected = DatetimeIndex(["1970-01-01 00:00:01"], tz="UTC") tm.assert_index_equal(result, expected) @@ -1508,7 +1508,7 @@ def test_to_datetime_unit(self): ) tm.assert_series_equal(result, expected) - result = to_datetime([1, 2, "NaT", pd.NaT, np.nan], unit="D") + result = to_datetime([1, 2, "NaT", NaT, np.nan], unit="D") expected = DatetimeIndex( [Timestamp("1970-01-02"), Timestamp("1970-01-03")] + ["NaT"] * 3 ) @@ -1593,20 +1593,20 @@ def test_to_datetime_with_apply(self, cache): # GH 5195 # with a format and coerce a single item to_datetime fails td = Series(["May 04", "Jun 02", "Dec 11"], index=[1, 2, 3]) - expected = pd.to_datetime(td, format="%b %y", cache=cache) - result = td.apply(pd.to_datetime, format="%b %y", cache=cache) + expected = to_datetime(td, format="%b %y", cache=cache) + result = td.apply(to_datetime, format="%b %y", cache=cache) tm.assert_series_equal(result, expected) td = Series(["May 04", "Jun 02", ""], index=[1, 2, 3]) msg = r"time data '' does not match format '%b %y' \(match\)" with pytest.raises(ValueError, match=msg): - pd.to_datetime(td, format="%b %y", errors="raise", cache=cache) + to_datetime(td, format="%b %y", errors="raise", cache=cache) with pytest.raises(ValueError, match=msg): - td.apply(pd.to_datetime, format="%b %y", errors="raise", cache=cache) - expected = pd.to_datetime(td, format="%b %y", errors="coerce", cache=cache) + td.apply(to_datetime, format="%b %y", errors="raise", cache=cache) + expected = to_datetime(td, format="%b %y", errors="coerce", cache=cache) result = td.apply( - lambda x: pd.to_datetime(x, format="%b %y", errors="coerce", cache=cache) + lambda x: to_datetime(x, format="%b %y", errors="coerce", cache=cache) ) tm.assert_series_equal(result, expected) @@ -1748,7 +1748,7 @@ def test_string_na_nat_conversion(self, cache): for i in range(5): x = series[i] if isna(x): - expected[i] = pd.NaT + expected[i] = NaT else: expected[i] = to_datetime(x, cache=cache) @@ -1772,7 +1772,7 @@ def test_string_na_nat_conversion(self, cache): @pytest.mark.parametrize("cache", [True, False]) def test_dti_constructor_numpy_timeunits(self, cache, dtype): # GH 9114 - base = pd.to_datetime( + base = to_datetime( ["2000-01-01T00:00", "2000-01-02T00:00", "NaT"], cache=cache ) @@ -1838,20 +1838,20 @@ def test_guess_datetime_format_for_array(self): class TestToDatetimeInferFormat: @pytest.mark.parametrize("cache", [True, False]) def test_to_datetime_infer_datetime_format_consistent_format(self, cache): - s = Series(pd.date_range("20000101", periods=50, freq="H")) + s = Series(date_range("20000101", periods=50, freq="H")) test_formats = ["%m-%d-%Y", "%m/%d/%Y %H:%M:%S.%f", "%Y-%m-%dT%H:%M:%S.%f"] for test_format in test_formats: s_as_dt_strings = s.apply(lambda x: x.strftime(test_format)) - with_format = pd.to_datetime( + with_format = to_datetime( s_as_dt_strings, format=test_format, cache=cache ) - no_infer = pd.to_datetime( + no_infer = to_datetime( s_as_dt_strings, infer_datetime_format=False, cache=cache ) - yes_infer = pd.to_datetime( + yes_infer = to_datetime( s_as_dt_strings, infer_datetime_format=True, cache=cache ) @@ -1871,15 +1871,15 @@ def test_to_datetime_infer_datetime_format_inconsistent_format(self, cache): # When the format is inconsistent, infer_datetime_format should just # fallback to the default parsing tm.assert_series_equal( - pd.to_datetime(s, infer_datetime_format=False, cache=cache), - pd.to_datetime(s, infer_datetime_format=True, cache=cache), + to_datetime(s, infer_datetime_format=False, cache=cache), + to_datetime(s, infer_datetime_format=True, cache=cache), ) s = Series(np.array(["Jan/01/2011", "Feb/01/2011", "Mar/01/2011"])) tm.assert_series_equal( - pd.to_datetime(s, infer_datetime_format=False, cache=cache), - pd.to_datetime(s, infer_datetime_format=True, cache=cache), + to_datetime(s, infer_datetime_format=False, cache=cache), + to_datetime(s, infer_datetime_format=True, cache=cache), ) @pytest.mark.parametrize("cache", [True, False]) @@ -1888,8 +1888,8 @@ def test_to_datetime_infer_datetime_format_series_with_nans(self, cache): np.array(["01/01/2011 00:00:00", np.nan, "01/03/2011 00:00:00", np.nan]) ) tm.assert_series_equal( - pd.to_datetime(s, infer_datetime_format=False, cache=cache), - pd.to_datetime(s, infer_datetime_format=True, cache=cache), + to_datetime(s, infer_datetime_format=False, cache=cache), + to_datetime(s, infer_datetime_format=True, cache=cache), ) @pytest.mark.parametrize("cache", [True, False]) @@ -1907,8 +1907,8 @@ def test_to_datetime_infer_datetime_format_series_start_with_nans(self, cache): ) tm.assert_series_equal( - pd.to_datetime(s, infer_datetime_format=False, cache=cache), - pd.to_datetime(s, infer_datetime_format=True, cache=cache), + to_datetime(s, infer_datetime_format=False, cache=cache), + to_datetime(s, infer_datetime_format=True, cache=cache), ) @pytest.mark.parametrize( @@ -1934,9 +1934,9 @@ def test_to_datetime_iso8601_noleading_0s(self, cache): Timestamp("2015-03-03"), ] ) - tm.assert_series_equal(pd.to_datetime(s, cache=cache), expected) + tm.assert_series_equal(to_datetime(s, cache=cache), expected) tm.assert_series_equal( - pd.to_datetime(s, format="%Y-%m-%d", cache=cache), expected + to_datetime(s, format="%Y-%m-%d", cache=cache), expected ) @@ -2280,7 +2280,7 @@ def epochs(epoch_1960, request): @pytest.fixture def julian_dates(): - return pd.date_range("2014-1-1", periods=10).to_julian_date().values + return date_range("2014-1-1", periods=10).to_julian_date().values class TestOrigin: @@ -2288,33 +2288,33 @@ def test_to_basic(self, julian_dates): # gh-11276, gh-11745 # for origin as julian - result = Series(pd.to_datetime(julian_dates, unit="D", origin="julian")) + result = Series(to_datetime(julian_dates, unit="D", origin="julian")) expected = Series( - pd.to_datetime(julian_dates - Timestamp(0).to_julian_date(), unit="D") + to_datetime(julian_dates - Timestamp(0).to_julian_date(), unit="D") ) tm.assert_series_equal(result, expected) - result = Series(pd.to_datetime([0, 1, 2], unit="D", origin="unix")) + result = Series(to_datetime([0, 1, 2], unit="D", origin="unix")) expected = Series( [Timestamp("1970-01-01"), Timestamp("1970-01-02"), Timestamp("1970-01-03")] ) tm.assert_series_equal(result, expected) # default - result = Series(pd.to_datetime([0, 1, 2], unit="D")) + result = Series(to_datetime([0, 1, 2], unit="D")) expected = Series( [Timestamp("1970-01-01"), Timestamp("1970-01-02"), Timestamp("1970-01-03")] ) tm.assert_series_equal(result, expected) def test_julian_round_trip(self): - result = pd.to_datetime(2456658, origin="julian", unit="D") + result = to_datetime(2456658, origin="julian", unit="D") assert result.to_julian_date() == 2456658 # out-of-bounds msg = "1 is Out of Bounds for origin='julian'" with pytest.raises(ValueError, match=msg): - pd.to_datetime(1, origin="julian", unit="D") + to_datetime(1, origin="julian", unit="D") def test_invalid_unit(self, units, julian_dates): @@ -2322,17 +2322,17 @@ def test_invalid_unit(self, units, julian_dates): if units != "D": msg = "unit must be 'D' for origin='julian'" with pytest.raises(ValueError, match=msg): - pd.to_datetime(julian_dates, unit=units, origin="julian") + to_datetime(julian_dates, unit=units, origin="julian") def test_invalid_origin(self): # need to have a numeric specified msg = "it must be numeric with a unit specified" with pytest.raises(ValueError, match=msg): - pd.to_datetime("2005-01-01", origin="1960-01-01") + to_datetime("2005-01-01", origin="1960-01-01") with pytest.raises(ValueError, match=msg): - pd.to_datetime("2005-01-01", origin="1960-01-01", unit="D") + to_datetime("2005-01-01", origin="1960-01-01", unit="D") def test_epoch(self, units, epochs, epoch_1960, units_from_epochs): @@ -2340,7 +2340,7 @@ def test_epoch(self, units, epochs, epoch_1960, units_from_epochs): [pd.Timedelta(x, unit=units) + epoch_1960 for x in units_from_epochs] ) - result = Series(pd.to_datetime(units_from_epochs, unit=units, origin=epochs)) + result = Series(to_datetime(units_from_epochs, unit=units, origin=epochs)) tm.assert_series_equal(result, expected) @pytest.mark.parametrize( @@ -2356,12 +2356,12 @@ def test_invalid_origins(self, origin, exc, units, units_from_epochs): msg = f"origin {origin} (is Out of Bounds|cannot be converted to a Timestamp)" with pytest.raises(exc, match=msg): - pd.to_datetime(units_from_epochs, unit=units, origin=origin) + to_datetime(units_from_epochs, unit=units, origin=origin) def test_invalid_origins_tzinfo(self): # GH16842 with pytest.raises(ValueError, match="must be tz-naive"): - pd.to_datetime(1, unit="D", origin=datetime(2000, 1, 1, tzinfo=pytz.utc)) + to_datetime(1, unit="D", origin=datetime(2000, 1, 1, tzinfo=pytz.utc)) @pytest.mark.parametrize("format", [None, "%Y-%m-%d %H:%M:%S"]) def test_to_datetime_out_of_bounds_with_format_arg(self, format): @@ -2374,15 +2374,15 @@ def test_processing_order(self): # make sure we handle out-of-bounds *before* # constructing the dates - result = pd.to_datetime(200 * 365, unit="D") + result = to_datetime(200 * 365, unit="D") expected = Timestamp("2169-11-13 00:00:00") assert result == expected - result = pd.to_datetime(200 * 365, unit="D", origin="1870-01-01") + result = to_datetime(200 * 365, unit="D", origin="1870-01-01") expected = Timestamp("2069-11-13 00:00:00") assert result == expected - result = pd.to_datetime(300 * 365, unit="D", origin="1870-01-01") + result = to_datetime(300 * 365, unit="D", origin="1870-01-01") expected = Timestamp("2169-10-20 00:00:00") assert result == expected @@ -2434,7 +2434,7 @@ def test_nullable_integer_to_datetime(): ser = ser.astype("Int64") ser_copy = ser.copy() - res = pd.to_datetime(ser, unit="ns") + res = to_datetime(ser, unit="ns") expected = Series( [ @@ -2455,12 +2455,12 @@ def test_na_to_datetime(nulls_fixture, klass): if isinstance(nulls_fixture, Decimal): with pytest.raises(TypeError, match="not convertible to datetime"): - pd.to_datetime(klass([nulls_fixture])) + to_datetime(klass([nulls_fixture])) else: - result = pd.to_datetime(klass([nulls_fixture])) + result = to_datetime(klass([nulls_fixture])) - assert result[0] is pd.NaT + assert result[0] is NaT def test_empty_string_datetime_coerce__format(): @@ -2469,26 +2469,26 @@ def test_empty_string_datetime_coerce__format(): format = "%m/%d/%Y" # coerce empty string to pd.NaT - result = pd.to_datetime(td, format=format, errors="coerce") - expected = Series(["2016-03-24", "2016-03-25", pd.NaT], dtype="datetime64[ns]") + result = to_datetime(td, format=format, errors="coerce") + expected = Series(["2016-03-24", "2016-03-25", NaT], dtype="datetime64[ns]") tm.assert_series_equal(expected, result) # raise an exception in case a format is given with pytest.raises(ValueError, match="does not match format"): - result = pd.to_datetime(td, format=format, errors="raise") + result = to_datetime(td, format=format, errors="raise") # don't raise an expection in case no format is given - result = pd.to_datetime(td, errors="raise") + result = to_datetime(td, errors="raise") tm.assert_series_equal(result, expected) def test_empty_string_datetime_coerce__unit(): # GH13044 # coerce empty string to pd.NaT - result = pd.to_datetime([1, ""], unit="s", errors="coerce") + result = to_datetime([1, ""], unit="s", errors="coerce") expected = DatetimeIndex(["1970-01-01 00:00:01", "NaT"], dtype="datetime64[ns]") tm.assert_index_equal(expected, result) # verify that no exception is raised even when errors='raise' is set - result = pd.to_datetime([1, ""], unit="s", errors="raise") + result = to_datetime([1, ""], unit="s", errors="raise") tm.assert_index_equal(expected, result) diff --git a/pandas/tests/tools/test_to_numeric.py b/pandas/tests/tools/test_to_numeric.py index 15ee296be0908..65aa189a3e965 100644 --- a/pandas/tests/tools/test_to_numeric.py +++ b/pandas/tests/tools/test_to_numeric.py @@ -585,7 +585,7 @@ def test_downcast_uint64(ser, expected): # see gh-14422: # BUG: to_numeric doesn't work uint64 numbers - result = pd.to_numeric(ser, downcast="unsigned") + result = to_numeric(ser, downcast="unsigned") tm.assert_series_equal(result, expected) @@ -640,8 +640,8 @@ def test_downcast_empty(dc1, dc2): # GH32493 tm.assert_numpy_array_equal( - pd.to_numeric([], downcast=dc1), - pd.to_numeric([], downcast=dc2), + to_numeric([], downcast=dc1), + to_numeric([], downcast=dc2), check_dtype=False, ) @@ -766,7 +766,7 @@ def test_to_numeric_from_nullable_string(values, expected): ) def test_downcast_nullable_numeric(data, input_dtype, downcast, expected_dtype): arr = pd.array(data, dtype=input_dtype) - result = pd.to_numeric(arr, downcast=downcast) + result = to_numeric(arr, downcast=downcast) expected = pd.array(data, dtype=expected_dtype) tm.assert_extension_array_equal(result, expected) @@ -776,7 +776,7 @@ def test_downcast_nullable_mask_is_copied(): arr = pd.array([1, 2, pd.NA], dtype="Int64") - result = pd.to_numeric(arr, downcast="integer") + result = to_numeric(arr, downcast="integer") expected = pd.array([1, 2, pd.NA], dtype="Int8") tm.assert_extension_array_equal(result, expected) diff --git a/pandas/tests/tools/test_to_timedelta.py b/pandas/tests/tools/test_to_timedelta.py index 0c4146aa6a534..efcca0df639f6 100644 --- a/pandas/tests/tools/test_to_timedelta.py +++ b/pandas/tests/tools/test_to_timedelta.py @@ -190,37 +190,37 @@ def test_to_timedelta_on_missing_values(self): # GH5438 timedelta_NaT = np.timedelta64("NaT") - actual = pd.to_timedelta(Series(["00:00:01", np.nan])) + actual = to_timedelta(Series(["00:00:01", np.nan])) expected = Series( [np.timedelta64(1000000000, "ns"), timedelta_NaT], dtype=" Date: Sun, 7 Mar 2021 12:46:01 +0530 Subject: [PATCH 02/14] Pre-Commit Fixes --- pandas/tests/tools/test_to_datetime.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index cda056e7f4c96..13768a2cd7a61 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -725,9 +725,7 @@ def test_to_datetime_utc_true( # Test scalar case as well for scalar, expected in zip(data, expected_data): - result = to_datetime( - scalar, format="%Y%m%d %H%M%S", utc=True, cache=cache - ) + result = to_datetime(scalar, format="%Y%m%d %H%M%S", utc=True, cache=cache) assert result == expected @pytest.mark.parametrize("cache", [True, False]) @@ -1772,9 +1770,7 @@ def test_string_na_nat_conversion(self, cache): @pytest.mark.parametrize("cache", [True, False]) def test_dti_constructor_numpy_timeunits(self, cache, dtype): # GH 9114 - base = to_datetime( - ["2000-01-01T00:00", "2000-01-02T00:00", "NaT"], cache=cache - ) + base = to_datetime(["2000-01-01T00:00", "2000-01-02T00:00", "NaT"], cache=cache) values = base.values.astype(dtype) @@ -1845,9 +1841,7 @@ def test_to_datetime_infer_datetime_format_consistent_format(self, cache): for test_format in test_formats: s_as_dt_strings = s.apply(lambda x: x.strftime(test_format)) - with_format = to_datetime( - s_as_dt_strings, format=test_format, cache=cache - ) + with_format = to_datetime(s_as_dt_strings, format=test_format, cache=cache) no_infer = to_datetime( s_as_dt_strings, infer_datetime_format=False, cache=cache ) @@ -1935,9 +1929,7 @@ def test_to_datetime_iso8601_noleading_0s(self, cache): ] ) tm.assert_series_equal(to_datetime(s, cache=cache), expected) - tm.assert_series_equal( - to_datetime(s, format="%Y-%m-%d", cache=cache), expected - ) + tm.assert_series_equal(to_datetime(s, format="%Y-%m-%d", cache=cache), expected) class TestDaysInMonth: From 5723060d1c754031dc3f658681b5f1213f816810 Mon Sep 17 00:00:00 2001 From: deepang17 <47976918+deepang17@users.noreply.github.com> Date: Sun, 7 Mar 2021 18:38:37 +0530 Subject: [PATCH 03/14] STYLE: Resolved Inconsistent namespace except test_algos.py - (#39992) --- .pre-commit-config.yaml | 2 +- pandas/tests/indexes/period/test_setops.py | 4 +- .../tests/indexing/interval/test_interval.py | 2 +- .../indexing/multiindex/test_multiindex.py | 2 +- .../tests/indexing/multiindex/test_slice.py | 8 +- pandas/tests/indexing/test_loc.py | 36 +++--- pandas/tests/io/formats/style/test_style.py | 4 +- pandas/tests/reductions/test_reductions.py | 118 +++++++++--------- pandas/tests/strings/test_cat.py | 3 +- 9 files changed, 86 insertions(+), 93 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0c90f8068cf81..8e3fcf97e09d1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -85,7 +85,7 @@ repos: entry: python scripts/check_for_inconsistent_pandas_namespace.py language: python types: [python] - files: ^pandas/tests/frame/ + files: ^pandas/tests/ - id: incorrect-code-directives name: Check for incorrect code block or IPython directives language: pygrep diff --git a/pandas/tests/indexes/period/test_setops.py b/pandas/tests/indexes/period/test_setops.py index 5b9528d185827..ce5c46dd55c0d 100644 --- a/pandas/tests/indexes/period/test_setops.py +++ b/pandas/tests/indexes/period/test_setops.py @@ -351,10 +351,10 @@ def test_intersection_equal_duplicates(self): def test_union_duplicates(self): # GH#36289 - idx = pd.period_range("2011-01-01", periods=2) + idx = period_range("2011-01-01", periods=2) idx_dup = idx.append(idx) - idx2 = pd.period_range("2011-01-02", periods=2) + idx2 = period_range("2011-01-02", periods=2) idx2_dup = idx2.append(idx2) result = idx_dup.union(idx2_dup) diff --git a/pandas/tests/indexing/interval/test_interval.py b/pandas/tests/indexing/interval/test_interval.py index eaf597e6bf978..eb13ec1f366af 100644 --- a/pandas/tests/indexing/interval/test_interval.py +++ b/pandas/tests/indexing/interval/test_interval.py @@ -124,7 +124,7 @@ def test_mi_intervalindex_slicing_with_scalar(self): pd.Index( ["RID1", "RID1", "RID2", "RID2", "RID1", "RID1", "RID2", "RID2"] ), - pd.IntervalIndex.from_arrays( + IntervalIndex.from_arrays( [0, 1, 10, 11, 0, 1, 10, 11], [1, 2, 11, 12, 1, 2, 11, 12] ), ] diff --git a/pandas/tests/indexing/multiindex/test_multiindex.py b/pandas/tests/indexing/multiindex/test_multiindex.py index e00b77003ebdc..41c2c61154f08 100644 --- a/pandas/tests/indexing/multiindex/test_multiindex.py +++ b/pandas/tests/indexing/multiindex/test_multiindex.py @@ -75,7 +75,7 @@ def test_nested_tuples_duplicates(self): dti = pd.to_datetime(["20190101", "20190101", "20190102"]) idx = Index(["a", "a", "c"]) - mi = pd.MultiIndex.from_arrays([dti, idx], names=["index1", "index2"]) + mi = MultiIndex.from_arrays([dti, idx], names=["index1", "index2"]) df = DataFrame({"c1": [1, 2, 3], "c2": [np.nan, np.nan, np.nan]}, index=mi) diff --git a/pandas/tests/indexing/multiindex/test_slice.py b/pandas/tests/indexing/multiindex/test_slice.py index 90f2d07ad77f6..42edaa2fe6c3a 100644 --- a/pandas/tests/indexing/multiindex/test_slice.py +++ b/pandas/tests/indexing/multiindex/test_slice.py @@ -534,9 +534,7 @@ def test_loc_axis_single_level_multi_col_indexing_multiindex_col_df(self): # GH29519 df = DataFrame( np.arange(27).reshape(3, 9), - columns=pd.MultiIndex.from_product( - [["a1", "a2", "a3"], ["b1", "b2", "b3"]] - ), + columns=MultiIndex.from_product([["a1", "a2", "a3"], ["b1", "b2", "b3"]]), ) result = df.loc(axis=1)["a1":"a2"] expected = df.iloc[:, :-3] @@ -548,9 +546,7 @@ def test_loc_axis_single_level_single_col_indexing_multiindex_col_df(self): # GH29519 df = DataFrame( np.arange(27).reshape(3, 9), - columns=pd.MultiIndex.from_product( - [["a1", "a2", "a3"], ["b1", "b2", "b3"]] - ), + columns=MultiIndex.from_product([["a1", "a2", "a3"], ["b1", "b2", "b3"]]), ) result = df.loc(axis=1)["a1"] expected = df.iloc[:, :3] diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 9dbce283d2a8f..1d5886085a5a5 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -525,10 +525,10 @@ def test_loc_setitem_consistency_slice_column_len(self): Region_1,Site_2,3977723089,A,5/20/2015 8:33,5/20/2015 9:09,Yes,No""" df = pd.read_csv(StringIO(data), header=[0, 1], index_col=[0, 1, 2]) - df.loc[:, ("Respondent", "StartDate")] = pd.to_datetime( + df.loc[:, ("Respondent", "StartDate")] = to_datetime( df.loc[:, ("Respondent", "StartDate")] ) - df.loc[:, ("Respondent", "EndDate")] = pd.to_datetime( + df.loc[:, ("Respondent", "EndDate")] = to_datetime( df.loc[:, ("Respondent", "EndDate")] ) df.loc[:, ("Respondent", "Duration")] = ( @@ -568,7 +568,7 @@ def test_loc_modify_datetime(self): {"date": [1485264372711, 1485265925110, 1540215845888, 1540282121025]} ) - df["date_dt"] = pd.to_datetime(df["date"], unit="ms", cache=True) + df["date_dt"] = to_datetime(df["date"], unit="ms", cache=True) df.loc[:, "date_dt_cp"] = df.loc[:, "date_dt"] df.loc[[2, 3], "date_dt_cp"] = df.loc[[2, 3], "date_dt"] @@ -584,7 +584,7 @@ def test_loc_modify_datetime(self): ) columns = ["date_dt", "date_dt_cp"] - expected[columns] = expected[columns].apply(pd.to_datetime) + expected[columns] = expected[columns].apply(to_datetime) tm.assert_frame_equal(df, expected) @@ -808,8 +808,8 @@ def test_loc_coercion(self): def test_setitem_new_key_tz(self, indexer_sl): # GH#12862 should not raise on assigning the second value vals = [ - pd.to_datetime(42).tz_localize("UTC"), - pd.to_datetime(666).tz_localize("UTC"), + to_datetime(42).tz_localize("UTC"), + to_datetime(666).tz_localize("UTC"), ] expected = Series(vals, index=["foo", "bar"]) @@ -1458,7 +1458,7 @@ def test_loc_getitem_access_none_value_in_multiindex(self): # GH#34318: test that you can access a None value using .loc # through a Multiindex - ser = Series([None], pd.MultiIndex.from_arrays([["Level1"], ["Level2"]])) + ser = Series([None], MultiIndex.from_arrays([["Level1"], ["Level2"]])) result = ser.loc[("Level1", "Level2")] assert result is None @@ -1474,7 +1474,7 @@ def test_loc_getitem_access_none_value_in_multiindex(self): def test_loc_setitem_multiindex_slice(self): # GH 34870 - index = pd.MultiIndex.from_tuples( + index = MultiIndex.from_tuples( zip( ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"], ["one", "two", "one", "two", "one", "two", "one", "two"], @@ -1629,14 +1629,14 @@ def test_loc_setitem_with_expansion_and_existing_dst(self): start = Timestamp("2017-10-29 00:00:00+0200", tz="Europe/Madrid") end = Timestamp("2017-10-29 03:00:00+0100", tz="Europe/Madrid") ts = Timestamp("2016-10-10 03:00:00", tz="Europe/Madrid") - idx = pd.date_range(start, end, closed="left", freq="H") + idx = date_range(start, end, closed="left", freq="H") assert ts not in idx # i.e. result.loc setitem is with-expansion result = DataFrame(index=idx, columns=["value"]) result.loc[ts, "value"] = 12 expected = DataFrame( [np.nan] * len(idx) + [12], - index=idx.append(pd.DatetimeIndex([ts])), + index=idx.append(DatetimeIndex([ts])), columns=["value"], dtype=object, ) @@ -1645,7 +1645,7 @@ def test_loc_setitem_with_expansion_and_existing_dst(self): def test_setitem_with_expansion(self): # indexing - setting an element df = DataFrame( - data=pd.to_datetime(["2015-03-30 20:12:32", "2015-03-12 00:11:11"]), + data=to_datetime(["2015-03-30 20:12:32", "2015-03-12 00:11:11"]), columns=["time"], ) df["new_col"] = ["new", "old"] @@ -1660,7 +1660,7 @@ def test_setitem_with_expansion(self): expected = Series([v[0], df.loc[1, "time"]], name="time") tm.assert_series_equal(df2.time, expected) - v = df.loc[df.new_col == "new", "time"] + pd.Timedelta("1s") + v = df.loc[df.new_col == "new", "time"] + Timedelta("1s") df.loc[df.new_col == "new", "time"] = v tm.assert_series_equal(df.loc[df.new_col == "new", "time"], v) @@ -2296,15 +2296,13 @@ def test_loc_axis_1_slice(): df = DataFrame( np.ones((10, 8)), index=tuple("ABCDEFGHIJ"), - columns=pd.MultiIndex.from_tuples(cols), + columns=MultiIndex.from_tuples(cols), ) result = df.loc(axis=1)[(2014, 9):(2015, 8)] expected = DataFrame( np.ones((10, 4)), index=tuple("ABCDEFGHIJ"), - columns=pd.MultiIndex.from_tuples( - [(2014, 9), (2014, 10), (2015, 7), (2015, 8)] - ), + columns=MultiIndex.from_tuples([(2014, 9), (2014, 10), (2015, 7), (2015, 8)]), ) tm.assert_frame_equal(result, expected) @@ -2312,7 +2310,7 @@ def test_loc_axis_1_slice(): def test_loc_set_dataframe_multiindex(): # GH 14592 expected = DataFrame( - "a", index=range(2), columns=pd.MultiIndex.from_product([range(2), range(2)]) + "a", index=range(2), columns=MultiIndex.from_product([range(2), range(2)]) ) result = expected.copy() result.loc[0, [(0, 1)]] = result.loc[0, [(0, 1)]] @@ -2340,7 +2338,7 @@ def test_loc_with_positional_slice_deprecation(): def test_loc_slice_disallows_positional(): # GH#16121, GH#24612, GH#31810 - dti = pd.date_range("2016-01-01", periods=3) + dti = date_range("2016-01-01", periods=3) df = DataFrame(np.random.random((3, 2)), index=dti) ser = df[0] @@ -2372,7 +2370,7 @@ def test_loc_datetimelike_mismatched_dtypes(): df = DataFrame( np.random.randn(5, 3), columns=["a", "b", "c"], - index=pd.date_range("2012", freq="H", periods=5), + index=date_range("2012", freq="H", periods=5), ) # create dataframe with non-unique DatetimeIndex df = df.iloc[[0, 2, 2, 3]].copy() diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index b938495ca9e31..0128d8282342e 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -84,13 +84,13 @@ def test_copy(self, do_changes, do_render): self.styler.set_table_attributes('class="foo" data-bar') self.styler.hidden_index = not self.styler.hidden_index self.styler.hide_columns("A") - classes = pd.DataFrame( + classes = DataFrame( [["favorite-val red", ""], [None, "blue my-val"]], index=self.df.index, columns=self.df.columns, ) self.styler.set_td_classes(classes) - ttips = pd.DataFrame( + ttips = DataFrame( data=[["Favorite", ""], [np.nan, "my"]], columns=self.df.columns, index=self.df.index, diff --git a/pandas/tests/reductions/test_reductions.py b/pandas/tests/reductions/test_reductions.py index cf6b2d372657d..2d7dddbc5ea42 100644 --- a/pandas/tests/reductions/test_reductions.py +++ b/pandas/tests/reductions/test_reductions.py @@ -83,16 +83,16 @@ def test_nanminmax(self, opname, dtype, val, index_or_series): # GH#7261 klass = index_or_series - if dtype in ["Int64", "boolean"] and klass == pd.Index: + if dtype in ["Int64", "boolean"] and klass == Index: pytest.skip("EAs can't yet be stored in an index") def check_missing(res): if dtype == "datetime64[ns]": - return res is pd.NaT + return res is NaT elif dtype == "Int64": return res is pd.NA else: - return pd.isna(res) + return isna(res) obj = klass([None], dtype=dtype) assert check_missing(getattr(obj, opname)()) @@ -120,7 +120,7 @@ def test_nanargminmax(self, opname, index_or_series): klass = index_or_series arg_op = "arg" + opname if klass is Index else "idx" + opname - obj = klass([pd.NaT, datetime(2011, 11, 1)]) + obj = klass([NaT, datetime(2011, 11, 1)]) assert getattr(obj, arg_op)() == 1 result = getattr(obj, arg_op)(skipna=False) if klass is Series: @@ -128,7 +128,7 @@ def test_nanargminmax(self, opname, index_or_series): else: assert result == -1 - obj = klass([pd.NaT, datetime(2011, 11, 1), pd.NaT]) + obj = klass([NaT, datetime(2011, 11, 1), NaT]) # check DatetimeIndex non-monotonic path assert getattr(obj, arg_op)() == 1 result = getattr(obj, arg_op)(skipna=False) @@ -145,8 +145,8 @@ def test_nanops_empty_object(self, opname, index_or_series, dtype): obj = klass([], dtype=dtype) - assert getattr(obj, opname)() is pd.NaT - assert getattr(obj, opname)(skipna=False) is pd.NaT + assert getattr(obj, opname)() is NaT + assert getattr(obj, opname)(skipna=False) is NaT with pytest.raises(ValueError, match="empty sequence"): getattr(obj, arg_op)() @@ -170,13 +170,13 @@ def test_argminmax(self): assert obj.argmin(skipna=False) == -1 assert obj.argmax(skipna=False) == -1 - obj = Index([pd.NaT, datetime(2011, 11, 1), datetime(2011, 11, 2), pd.NaT]) + obj = Index([NaT, datetime(2011, 11, 1), datetime(2011, 11, 2), NaT]) assert obj.argmin() == 1 assert obj.argmax() == 2 assert obj.argmin(skipna=False) == -1 assert obj.argmax(skipna=False) == -1 - obj = Index([pd.NaT]) + obj = Index([NaT]) assert obj.argmin() == -1 assert obj.argmax() == -1 assert obj.argmin(skipna=False) == -1 @@ -186,7 +186,7 @@ def test_argminmax(self): def test_same_tz_min_max_axis_1(self, op, expected_col): # GH 10390 df = DataFrame( - pd.date_range("2016-01-01 00:00:00", periods=3, tz="UTC"), columns=["a"] + date_range("2016-01-01 00:00:00", periods=3, tz="UTC"), columns=["a"] ) df["b"] = df.a.subtract(Timedelta(seconds=3600)) result = getattr(df, op)(axis=1) @@ -262,13 +262,13 @@ def test_minmax_timedelta64(self): def test_minmax_timedelta_empty_or_na(self, op): # Return NaT obj = TimedeltaIndex([]) - assert getattr(obj, op)() is pd.NaT + assert getattr(obj, op)() is NaT - obj = TimedeltaIndex([pd.NaT]) - assert getattr(obj, op)() is pd.NaT + obj = TimedeltaIndex([NaT]) + assert getattr(obj, op)() is NaT - obj = TimedeltaIndex([pd.NaT, pd.NaT, pd.NaT]) - assert getattr(obj, op)() is pd.NaT + obj = TimedeltaIndex([NaT, NaT, NaT]) + assert getattr(obj, op)() is NaT def test_numpy_minmax_timedelta64(self): td = timedelta_range("16815 days", "16820 days", freq="D") @@ -373,7 +373,7 @@ def test_minmax_tz(self, tz_naive_fixture): # non-monotonic idx2 = DatetimeIndex( - ["2011-01-01", pd.NaT, "2011-01-03", "2011-01-02", pd.NaT], tz=tz + ["2011-01-01", NaT, "2011-01-03", "2011-01-02", NaT], tz=tz ) assert not idx2.is_monotonic @@ -387,13 +387,13 @@ def test_minmax_tz(self, tz_naive_fixture): def test_minmax_nat_datetime64(self, op): # Return NaT obj = DatetimeIndex([]) - assert pd.isna(getattr(obj, op)()) + assert isna(getattr(obj, op)()) - obj = DatetimeIndex([pd.NaT]) - assert pd.isna(getattr(obj, op)()) + obj = DatetimeIndex([NaT]) + assert isna(getattr(obj, op)()) - obj = DatetimeIndex([pd.NaT, pd.NaT, pd.NaT]) - assert pd.isna(getattr(obj, op)()) + obj = DatetimeIndex([NaT, NaT, NaT]) + assert isna(getattr(obj, op)()) def test_numpy_minmax_integer(self): # GH#26125 @@ -449,7 +449,7 @@ def test_numpy_minmax_range(self): # is the same as basic integer index def test_numpy_minmax_datetime64(self): - dr = pd.date_range(start="2016-01-15", end="2016-01-20") + dr = date_range(start="2016-01-15", end="2016-01-20") assert np.min(dr) == Timestamp("2016-01-15 00:00:00", freq="D") assert np.max(dr) == Timestamp("2016-01-20 00:00:00", freq="D") @@ -588,7 +588,7 @@ def test_empty(self, method, unit, use_bottleneck, dtype): assert result == unit result = getattr(s, method)(min_count=1) - assert pd.isna(result) + assert isna(result) # Skipna, default result = getattr(s, method)(skipna=True) @@ -599,13 +599,13 @@ def test_empty(self, method, unit, use_bottleneck, dtype): assert result == unit result = getattr(s, method)(skipna=True, min_count=1) - assert pd.isna(result) + assert isna(result) result = getattr(s, method)(skipna=False, min_count=0) assert result == unit result = getattr(s, method)(skipna=False, min_count=1) - assert pd.isna(result) + assert isna(result) # All-NA s = Series([np.nan], dtype=dtype) @@ -618,7 +618,7 @@ def test_empty(self, method, unit, use_bottleneck, dtype): assert result == unit result = getattr(s, method)(min_count=1) - assert pd.isna(result) + assert isna(result) # Skipna, default result = getattr(s, method)(skipna=True) @@ -629,7 +629,7 @@ def test_empty(self, method, unit, use_bottleneck, dtype): assert result == unit result = getattr(s, method)(skipna=True, min_count=1) - assert pd.isna(result) + assert isna(result) # Mix of valid, empty s = Series([np.nan, 1], dtype=dtype) @@ -657,18 +657,18 @@ def test_empty(self, method, unit, use_bottleneck, dtype): s = Series([1], dtype=dtype) result = getattr(s, method)(min_count=2) - assert pd.isna(result) + assert isna(result) result = getattr(s, method)(skipna=False, min_count=2) - assert pd.isna(result) + assert isna(result) s = Series([np.nan], dtype=dtype) result = getattr(s, method)(min_count=2) - assert pd.isna(result) + assert isna(result) s = Series([np.nan, 1], dtype=dtype) result = getattr(s, method)(min_count=2) - assert pd.isna(result) + assert isna(result) @pytest.mark.parametrize("method, unit", [("sum", 0.0), ("prod", 1.0)]) def test_empty_multi(self, method, unit): @@ -716,7 +716,7 @@ def test_ops_consistency_on_empty(self, method): # float result = getattr(Series(dtype=float), method)() - assert pd.isna(result) + assert isna(result) # timedelta64[ns] tdser = Series([], dtype="m8[ns]") @@ -732,7 +732,7 @@ def test_ops_consistency_on_empty(self, method): getattr(tdser, method)() else: result = getattr(tdser, method)() - assert result is pd.NaT + assert result is NaT def test_nansum_buglet(self): ser = Series([1.0, np.nan], index=[0, 1]) @@ -770,10 +770,10 @@ def test_sum_overflow(self, use_bottleneck): def test_empty_timeseries_reductions_return_nat(self): # covers GH#11245 for dtype in ("m8[ns]", "m8[ns]", "M8[ns]", "M8[ns, UTC]"): - assert Series([], dtype=dtype).min() is pd.NaT - assert Series([], dtype=dtype).max() is pd.NaT - assert Series([], dtype=dtype).min(skipna=False) is pd.NaT - assert Series([], dtype=dtype).max(skipna=False) is pd.NaT + assert Series([], dtype=dtype).min() is NaT + assert Series([], dtype=dtype).max() is NaT + assert Series([], dtype=dtype).min(skipna=False) is NaT + assert Series([], dtype=dtype).max(skipna=False) is NaT def test_numpy_argmin(self): # See GH#16830 @@ -820,7 +820,7 @@ def test_idxmin(self): # skipna or no assert string_series[string_series.idxmin()] == string_series.min() - assert pd.isna(string_series.idxmin(skipna=False)) + assert isna(string_series.idxmin(skipna=False)) # no NaNs nona = string_series.dropna() @@ -829,10 +829,10 @@ def test_idxmin(self): # all NaNs allna = string_series * np.nan - assert pd.isna(allna.idxmin()) + assert isna(allna.idxmin()) # datetime64[ns] - s = Series(pd.date_range("20130102", periods=6)) + s = Series(date_range("20130102", periods=6)) result = s.idxmin() assert result == 0 @@ -850,7 +850,7 @@ def test_idxmax(self): # skipna or no assert string_series[string_series.idxmax()] == string_series.max() - assert pd.isna(string_series.idxmax(skipna=False)) + assert isna(string_series.idxmax(skipna=False)) # no NaNs nona = string_series.dropna() @@ -859,7 +859,7 @@ def test_idxmax(self): # all NaNs allna = string_series * np.nan - assert pd.isna(allna.idxmax()) + assert isna(allna.idxmax()) from pandas import date_range @@ -1010,7 +1010,7 @@ def test_any_all_datetimelike(self): def test_timedelta64_analytics(self): # index min/max - dti = pd.date_range("2012-1-1", periods=3, freq="D") + dti = date_range("2012-1-1", periods=3, freq="D") td = Series(dti) - Timestamp("20120101") result = td.idxmin() @@ -1030,8 +1030,8 @@ def test_timedelta64_analytics(self): assert result == 2 # abs - s1 = Series(pd.date_range("20120101", periods=3)) - s2 = Series(pd.date_range("20120102", periods=3)) + s1 = Series(date_range("20120101", periods=3)) + s2 = Series(date_range("20120102", periods=3)) expected = Series(s2 - s1) result = np.abs(s1 - s2) @@ -1108,35 +1108,35 @@ class TestDatetime64SeriesReductions: @pytest.mark.parametrize( "nat_ser", [ - Series([pd.NaT, pd.NaT]), - Series([pd.NaT, Timedelta("nat")]), + Series([NaT, NaT]), + Series([NaT, Timedelta("nat")]), Series([Timedelta("nat"), Timedelta("nat")]), ], ) def test_minmax_nat_series(self, nat_ser): # GH#23282 - assert nat_ser.min() is pd.NaT - assert nat_ser.max() is pd.NaT - assert nat_ser.min(skipna=False) is pd.NaT - assert nat_ser.max(skipna=False) is pd.NaT + assert nat_ser.min() is NaT + assert nat_ser.max() is NaT + assert nat_ser.min(skipna=False) is NaT + assert nat_ser.max(skipna=False) is NaT @pytest.mark.parametrize( "nat_df", [ - DataFrame([pd.NaT, pd.NaT]), - DataFrame([pd.NaT, Timedelta("nat")]), + DataFrame([NaT, NaT]), + DataFrame([NaT, Timedelta("nat")]), DataFrame([Timedelta("nat"), Timedelta("nat")]), ], ) def test_minmax_nat_dataframe(self, nat_df): # GH#23282 - assert nat_df.min()[0] is pd.NaT - assert nat_df.max()[0] is pd.NaT - assert nat_df.min(skipna=False)[0] is pd.NaT - assert nat_df.max(skipna=False)[0] is pd.NaT + assert nat_df.min()[0] is NaT + assert nat_df.max()[0] is NaT + assert nat_df.min(skipna=False)[0] is NaT + assert nat_df.max(skipna=False)[0] is NaT def test_min_max(self): - rng = pd.date_range("1/1/2000", "12/31/2000") + rng = date_range("1/1/2000", "12/31/2000") rng2 = rng.take(np.random.permutation(len(rng))) the_min = rng2.min() @@ -1150,7 +1150,7 @@ def test_min_max(self): assert rng.max() == rng[-1] def test_min_max_series(self): - rng = pd.date_range("1/1/2000", periods=10, freq="4h") + rng = date_range("1/1/2000", periods=10, freq="4h") lvls = ["A", "A", "A", "B", "B", "B", "C", "C", "C", "C"] df = DataFrame({"TS": rng, "V": np.random.randn(len(rng)), "L": lvls}) diff --git a/pandas/tests/strings/test_cat.py b/pandas/tests/strings/test_cat.py index cdaccf0dad8e6..68aa6b3c4a136 100644 --- a/pandas/tests/strings/test_cat.py +++ b/pandas/tests/strings/test_cat.py @@ -1,7 +1,6 @@ import numpy as np import pytest -import pandas as pd from pandas import ( DataFrame, Index, @@ -366,7 +365,7 @@ def test_cat_on_filtered_index(): assert str_multiple.loc[1] == "2011 2 2" -@pytest.mark.parametrize("klass", [tuple, list, np.array, pd.Series, pd.Index]) +@pytest.mark.parametrize("klass", [tuple, list, np.array, Series, Index]) def test_cat_different_classes(klass): # https://github.com/pandas-dev/pandas/issues/33425 s = Series(["a", "b", "c"]) From 6bc371c3769b72dfe89632cbdd1da139a3e8112a Mon Sep 17 00:00:00 2001 From: deepang17 <47976918+deepang17@users.noreply.github.com> Date: Sun, 7 Mar 2021 19:28:25 +0530 Subject: [PATCH 04/14] STYLE: Resolved Inconsistent namespace by excluding test_algos.py --- .pre-commit-config.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8e3fcf97e09d1..f10810635a660 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -86,6 +86,7 @@ repos: language: python types: [python] files: ^pandas/tests/ + exclude: ^pandas/tests/test_algos.py$ - id: incorrect-code-directives name: Check for incorrect code block or IPython directives language: pygrep From 06387fb5beeca44a42036364652b17fbdc5cab12 Mon Sep 17 00:00:00 2001 From: deepang17 <47976918+deepang17@users.noreply.github.com> Date: Sun, 7 Mar 2021 20:08:06 +0530 Subject: [PATCH 05/14] test_algos.py fixes --- .pre-commit-config.yaml | 1 - pandas/tests/test_algos.py | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f10810635a660..8e3fcf97e09d1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -86,7 +86,6 @@ repos: language: python types: [python] files: ^pandas/tests/ - exclude: ^pandas/tests/test_algos.py$ - id: incorrect-code-directives name: Check for incorrect code block or IPython directives language: pygrep diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index 26d336bee65ea..cc376c3c9f237 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -1491,10 +1491,10 @@ def test_unique_index(self): ([("a", 1), ("b", 2), ("a", 3), ("a", 1)], [("a", 1), ("b", 2), ("a", 3)]), ], ) - def test_unique_tuples(self, arr, unique): + def test_unique_tuples(self, arr, uniques): # https://github.com/pandas-dev/pandas/issues/16519 - expected = np.empty(len(unique), dtype=object) - expected[:] = unique + expected = np.empty(len(uniques), dtype=object) + expected[:] = uniques result = pd.unique(arr) tm.assert_numpy_array_equal(result, expected) From 9c631eb12c33d0284786c5cab7e77b3b10f81873 Mon Sep 17 00:00:00 2001 From: deepang17 <47976918+deepang17@users.noreply.github.com> Date: Mon, 8 Mar 2021 23:02:25 +0530 Subject: [PATCH 06/14] Updated test_algos.py --- pandas/tests/test_algos.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index cc376c3c9f237..876df69ae7f63 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -1478,7 +1478,7 @@ def test_unique_index(self): ) @pytest.mark.parametrize( - "arr, unique", + "arr, uniques", [ ( [(0, 0), (0, 1), (1, 0), (1, 1), (0, 0), (0, 1), (1, 0), (1, 1)], From 6af3b2d80a689c4b11f17016b360f67824935d5c Mon Sep 17 00:00:00 2001 From: deepang17 <47976918+deepang17@users.noreply.github.com> Date: Tue, 9 Mar 2021 00:55:53 +0530 Subject: [PATCH 07/14] STYLE: inconsistent-namespace-usage fixes --- pandas/tests/arithmetic/test_interval.py | 10 +++--- pandas/tests/arrays/sparse/test_array.py | 6 ++-- pandas/tests/arrays/string_/test_string.py | 16 +++++----- pandas/tests/arrays/test_datetimelike.py | 34 ++++++++++----------- pandas/tests/base/test_conversion.py | 34 ++++++++++----------- pandas/tests/dtypes/test_inference.py | 6 ++-- pandas/tests/extension/base/getitem.py | 10 +++--- pandas/tests/indexing/test_check_indexer.py | 20 ++++++------ pandas/tests/series/test_ufunc.py | 26 ++++++++-------- 9 files changed, 81 insertions(+), 81 deletions(-) diff --git a/pandas/tests/arithmetic/test_interval.py b/pandas/tests/arithmetic/test_interval.py index 46db9100b8b93..0dd42f95cdeaf 100644 --- a/pandas/tests/arithmetic/test_interval.py +++ b/pandas/tests/arithmetic/test_interval.py @@ -98,12 +98,12 @@ def interval_constructor(self, request): """ return request.param - def elementwise_comparison(self, op, array, other): + def elementwise_comparison(self, op, arrays, other): """ Helper that performs elementwise comparisons between `array` and `other` """ - other = other if is_list_like(other) else [other] * len(array) - expected = np.array([op(x, y) for x, y in zip(array, other)]) + other = other if is_list_like(other) else [other] * len(arrays) + expected = np.array([op(x, y) for x, y in zip(arrays, other)]) if isinstance(other, Series): return Series(expected, index=other.index) return expected @@ -234,8 +234,8 @@ def test_compare_list_like_nan(self, op, array, nulls_fixture, request): period_range("2017-01-01", periods=4, freq="D"), Categorical(list("abab")), Categorical(date_range("2017-01-01", periods=4)), - pd.array(list("abcd")), - pd.array(["foo", 3.14, None, object()]), + array(list("abcd")), + array(["foo", 3.14, None, object()]), ], ids=lambda x: str(x.dtype), ) diff --git a/pandas/tests/arrays/sparse/test_array.py b/pandas/tests/arrays/sparse/test_array.py index 26106f52c21c5..9a0192b4b7cc1 100644 --- a/pandas/tests/arrays/sparse/test_array.py +++ b/pandas/tests/arrays/sparse/test_array.py @@ -522,7 +522,7 @@ def test_astype_all(self, any_real_dtype): tm.assert_numpy_array_equal(np.asarray(res.to_dense()), vals.astype(typ)) @pytest.mark.parametrize( - "array, dtype, expected", + "arrays, dtype, expected", [ ( SparseArray([0, 1]), @@ -557,8 +557,8 @@ def test_astype_all(self, any_real_dtype): ), ], ) - def test_astype_more(self, array, dtype, expected): - result = array.astype(dtype) + def test_astype_more(self, arrays, dtype, expected): + result = arrays.astype(dtype) tm.assert_sp_array_equal(result, expected) def test_astype_nan_raises(self): diff --git a/pandas/tests/arrays/string_/test_string.py b/pandas/tests/arrays/string_/test_string.py index d5254adc1ee24..7ad27b609b0d0 100644 --- a/pandas/tests/arrays/string_/test_string.py +++ b/pandas/tests/arrays/string_/test_string.py @@ -223,31 +223,31 @@ def test_mul(dtype, request): @pytest.mark.xfail(reason="GH-28527") def test_add_strings(dtype): - array = pd.array(["a", "b", "c", "d"], dtype=dtype) + arrs = pd.array(["a", "b", "c", "d"], dtype=dtype) df = pd.DataFrame([["t", "u", "v", "w"]]) - assert array.__add__(df) is NotImplemented + assert arrs.__add__(df) is NotImplemented - result = array + df + result = arrs + df expected = pd.DataFrame([["at", "bu", "cv", "dw"]]).astype(dtype) tm.assert_frame_equal(result, expected) - result = df + array + result = df + arrs expected = pd.DataFrame([["ta", "ub", "vc", "wd"]]).astype(dtype) tm.assert_frame_equal(result, expected) @pytest.mark.xfail(reason="GH-28527") def test_add_frame(dtype): - array = pd.array(["a", "b", np.nan, np.nan], dtype=dtype) + arrs = pd.array(["a", "b", np.nan, np.nan], dtype=dtype) df = pd.DataFrame([["x", np.nan, "y", np.nan]]) - assert array.__add__(df) is NotImplemented + assert arrs.__add__(df) is NotImplemented - result = array + df + result = arrs + df expected = pd.DataFrame([["ax", np.nan, np.nan, np.nan]]).astype(dtype) tm.assert_frame_equal(result, expected) - result = df + array + result = df + arrs expected = pd.DataFrame([["xa", np.nan, np.nan, np.nan]]).astype(dtype) tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index ce39de88dd0dc..65bd5abe10451 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -1160,7 +1160,7 @@ def test_strftime_nat(self): @pytest.mark.parametrize( - "array,casting_nats", + "arrays,casting_nats", [ ( TimedeltaIndex(["1 Day", "3 Hours", "NaT"])._data, @@ -1174,17 +1174,17 @@ def test_strftime_nat(self): ], ids=lambda x: type(x).__name__, ) -def test_casting_nat_setitem_array(array, casting_nats): - expected = type(array)._from_sequence([NaT, array[1], array[2]]) +def test_casting_nat_setitem_array(arrays, casting_nats): + expected = type(arrays)._from_sequence([NaT, arrays[1], arrays[2]]) for nat in casting_nats: - arr = array.copy() + arr = arrays.copy() arr[0] = nat tm.assert_equal(arr, expected) @pytest.mark.parametrize( - "array,non_casting_nats", + "arrays,non_casting_nats", [ ( TimedeltaIndex(["1 Day", "3 Hours", "NaT"])._data, @@ -1201,7 +1201,7 @@ def test_casting_nat_setitem_array(array, casting_nats): ], ids=lambda x: type(x).__name__, ) -def test_invalid_nat_setitem_array(array, non_casting_nats): +def test_invalid_nat_setitem_array(arrays, non_casting_nats): msg = ( "value should be a '(Timestamp|Timedelta|Period)', 'NaT', or array of those. " "Got '(timedelta64|datetime64|int)' instead." @@ -1209,42 +1209,42 @@ def test_invalid_nat_setitem_array(array, non_casting_nats): for nat in non_casting_nats: with pytest.raises(TypeError, match=msg): - array[0] = nat + arrays[0] = nat @pytest.mark.parametrize( - "array", + "arrays", [ pd.date_range("2000", periods=4).array, pd.timedelta_range("2000", periods=4).array, ], ) -def test_to_numpy_extra(array): +def test_to_numpy_extra(arrays): if np_version_under1p18: # np.isnan(NaT) raises, so use pandas' isnan = pd.isna else: isnan = np.isnan - array[0] = NaT - original = array.copy() + arrays[0] = NaT + original = arrays.copy() - result = array.to_numpy() + result = arrays.to_numpy() assert isnan(result[0]) - result = array.to_numpy(dtype="int64") + result = arrays.to_numpy(dtype="int64") assert result[0] == -9223372036854775808 - result = array.to_numpy(dtype="int64", na_value=0) + result = arrays.to_numpy(dtype="int64", na_value=0) assert result[0] == 0 - result = array.to_numpy(na_value=array[1].to_numpy()) + result = arrays.to_numpy(na_value=arrays[1].to_numpy()) assert result[0] == result[1] - result = array.to_numpy(na_value=array[1].to_numpy(copy=False)) + result = arrays.to_numpy(na_value=arrays[1].to_numpy(copy=False)) assert result[0] == result[1] - tm.assert_equal(array, original) + tm.assert_equal(arrays, original) @pytest.mark.parametrize("as_index", [True, False]) diff --git a/pandas/tests/base/test_conversion.py b/pandas/tests/base/test_conversion.py index bb1ff2abd9bc4..9d7a5195fa099 100644 --- a/pandas/tests/base/test_conversion.py +++ b/pandas/tests/base/test_conversion.py @@ -177,7 +177,7 @@ def test_iter_box(self): @pytest.mark.parametrize( - "array, expected_type, dtype", + "arrays, expected_type, dtype", [ (np.array([0, 1], dtype=np.int64), np.ndarray, "int64"), (np.array(["a", "b"]), np.ndarray, "object"), @@ -216,9 +216,9 @@ def test_iter_box(self): ), ], ) -def test_values_consistent(array, expected_type, dtype): - l_values = Series(array)._values - r_values = pd.Index(array)._values +def test_values_consistent(arrays, expected_type, dtype): + l_values = Series(arrays)._values + r_values = pd.Index(arrays)._values assert type(l_values) is expected_type assert type(l_values) is type(r_values) @@ -245,7 +245,7 @@ def test_numpy_array_all_dtypes(any_numpy_dtype): @pytest.mark.parametrize( - "array, attr", + "arrays, attr", [ (pd.Categorical(["a", "b"]), "_codes"), (pd.core.arrays.period_array(["2000", "2001"], freq="D"), "_data"), @@ -265,17 +265,17 @@ def test_numpy_array_all_dtypes(any_numpy_dtype): ), ], ) -def test_array(array, attr, index_or_series): +def test_array(arrays, attr, index_or_series): box = index_or_series - if array.dtype.name in ("Int64", "Sparse[int64, 0]") and box is pd.Index: - pytest.skip(f"No index type for {array.dtype}") - result = box(array, copy=False).array + if arrays.dtype.name in ("Int64", "Sparse[int64, 0]") and box is pd.Index: + pytest.skip(f"No index type for {arrays.dtype}") + result = box(arrays, copy=False).arrays if attr: - array = getattr(array, attr) + arrays = getattr(arrays, attr) result = getattr(result, attr) - assert result is array + assert result is arrays def test_array_multiindex_raises(): @@ -286,7 +286,7 @@ def test_array_multiindex_raises(): @pytest.mark.parametrize( - "array, expected", + "arrays, expected", [ (np.array([1, 2], dtype=np.int64), np.array([1, 2], dtype=np.int64)), (pd.Categorical(["a", "b"]), np.array(["a", "b"], dtype=object)), @@ -337,14 +337,14 @@ def test_array_multiindex_raises(): ), ], ) -def test_to_numpy(array, expected, index_or_series_or_array, request): +def test_to_numpy(arrays, expected, index_or_series_or_array, request): box = index_or_series_or_array - thing = box(array) + thing = box(arrays) - if array.dtype.name in ("Int64", "Sparse[int64, 0]") and box is pd.Index: - pytest.skip(f"No index type for {array.dtype}") + if arrays.dtype.name in ("Int64", "Sparse[int64, 0]") and box is pd.Index: + pytest.skip(f"No index type for {arrays.dtype}") - if array.dtype.name == "int64" and box is pd.array: + if arrays.dtype.name == "int64" and box is pd.array: mark = pytest.mark.xfail(reason="thing is Int64 and to_numpy() returns object") request.node.add_marker(mark) diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index 78a62c832833f..37a990c2dab65 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -616,9 +616,9 @@ def test_maybe_convert_objects_bool_nan(self): def test_mixed_dtypes_remain_object_array(self): # GH14956 - array = np.array([datetime(2015, 1, 1, tzinfo=pytz.utc), 1], dtype=object) - result = lib.maybe_convert_objects(array, convert_datetime=True) - tm.assert_numpy_array_equal(result, array) + arrays = np.array([datetime(2015, 1, 1, tzinfo=pytz.utc), 1], dtype=object) + result = lib.maybe_convert_objects(arrays, convert_datetime=True) + tm.assert_numpy_array_equal(result, arrays) class TestTypeInference: diff --git a/pandas/tests/extension/base/getitem.py b/pandas/tests/extension/base/getitem.py index 286ed9c736f31..ebd5bb7fb113c 100644 --- a/pandas/tests/extension/base/getitem.py +++ b/pandas/tests/extension/base/getitem.py @@ -324,11 +324,11 @@ def test_take_non_na_fill_value(self, data_missing): fill_value = data_missing[1] # valid na = data_missing[0] - array = data_missing._from_sequence( + arrays = data_missing._from_sequence( [na, fill_value, na], dtype=data_missing.dtype ) - result = array.take([-1, 1], fill_value=fill_value, allow_fill=True) - expected = array.take([1, 1]) + result = arrays.take([-1, 1], fill_value=fill_value, allow_fill=True) + expected = arrays.take([1, 1]) self.assert_extension_array_equal(result, expected) def test_take_pandas_style_negative_raises(self, data, na_value): @@ -375,8 +375,8 @@ def test_reindex_non_na_fill_value(self, data_missing): valid = data_missing[1] na = data_missing[0] - array = data_missing._from_sequence([na, valid], dtype=data_missing.dtype) - ser = pd.Series(array) + arrays = data_missing._from_sequence([na, valid], dtype=data_missing.dtype) + ser = pd.Series(arrays) result = ser.reindex([0, 1, 2], fill_value=valid) expected = pd.Series( data_missing._from_sequence([na, valid, valid], dtype=data_missing.dtype) diff --git a/pandas/tests/indexing/test_check_indexer.py b/pandas/tests/indexing/test_check_indexer.py index 865ecb129cdfa..24a7159bc5974 100644 --- a/pandas/tests/indexing/test_check_indexer.py +++ b/pandas/tests/indexing/test_check_indexer.py @@ -26,8 +26,8 @@ ], ) def test_valid_input(indexer, expected): - array = np.array([1, 2, 3]) - result = check_array_indexer(array, indexer) + arrays = np.array([1, 2, 3]) + result = check_array_indexer(arrays, indexer) tm.assert_numpy_array_equal(result, expected) @@ -53,22 +53,22 @@ def test_boolean_na_returns_indexer(indexer): ], ) def test_bool_raise_length(indexer): - array = np.array([1, 2, 3]) + arrays = np.array([1, 2, 3]) msg = "Boolean index has wrong length" with pytest.raises(IndexError, match=msg): - check_array_indexer(array, indexer) + check_array_indexer(arrays, indexer) @pytest.mark.parametrize( "indexer", [[0, 1, None], pd.array([0, 1, pd.NA], dtype="Int64")] ) def test_int_raise_missing_values(indexer): - array = np.array([1, 2, 3]) + arrays = np.array([1, 2, 3]) msg = "Cannot index with an integer indexer containing NA values" with pytest.raises(ValueError, match=msg): - check_array_indexer(array, indexer) + check_array_indexer(arrays, indexer) @pytest.mark.parametrize( @@ -82,16 +82,16 @@ def test_int_raise_missing_values(indexer): ], ) def test_raise_invalid_array_dtypes(indexer): - array = np.array([1, 2, 3]) + arrays = np.array([1, 2, 3]) msg = "arrays used as indices must be of integer or boolean type" with pytest.raises(IndexError, match=msg): - check_array_indexer(array, indexer) + check_array_indexer(arrays, indexer) @pytest.mark.parametrize("indexer", [None, Ellipsis, slice(0, 3), (None,)]) def test_pass_through_non_array_likes(indexer): - array = np.array([1, 2, 3]) + arrays = np.array([1, 2, 3]) - result = check_array_indexer(array, indexer) + result = check_array_indexer(arrays, indexer) assert result == indexer diff --git a/pandas/tests/series/test_ufunc.py b/pandas/tests/series/test_ufunc.py index a5aa319e4772f..19102f3098c6b 100644 --- a/pandas/tests/series/test_ufunc.py +++ b/pandas/tests/series/test_ufunc.py @@ -31,17 +31,17 @@ def arrays_for_binary_ufunc(): @pytest.mark.parametrize("sparse", SPARSE, ids=SPARSE_IDS) def test_unary_ufunc(ufunc, sparse): # Test that ufunc(pd.Series) == pd.Series(ufunc) - array = np.random.randint(0, 10, 10, dtype="int64") - array[::2] = 0 + arrays = np.random.randint(0, 10, 10, dtype="int64") + arrays[::2] = 0 if sparse: - array = SparseArray(array, dtype=pd.SparseDtype("int64", 0)) + arrays = SparseArray(arrays, dtype=pd.SparseDtype("int64", 0)) index = list(string.ascii_letters[:10]) name = "name" - series = pd.Series(array, index=index, name=name) + series = pd.Series(arrays, index=index, name=name) result = ufunc(series) - expected = pd.Series(ufunc(array), index=index, name=name) + expected = pd.Series(ufunc(arrays), index=index, name=name) tm.assert_series_equal(result, expected) @@ -148,14 +148,14 @@ def test_binary_ufunc_scalar(ufunc, sparse, flip, arrays_for_binary_ufunc): # Test that # * ufunc(pd.Series, scalar) == pd.Series(ufunc(array, scalar)) # * ufunc(pd.Series, scalar) == ufunc(scalar, pd.Series) - array, _ = arrays_for_binary_ufunc + arrays, _ = arrays_for_binary_ufunc if sparse: - array = SparseArray(array) + arrays = SparseArray(arrays) other = 2 - series = pd.Series(array, name="name") + series = pd.Series(arrays, name="name") series_args = (series, other) - array_args = (array, other) + array_args = (arrays, other) if flip: series_args = tuple(reversed(series_args)) @@ -207,14 +207,14 @@ def test_multiple_output_binary_ufuncs(ufunc, sparse, shuffle, arrays_for_binary def test_multiple_output_ufunc(sparse, arrays_for_binary_ufunc): # Test that the same conditions from unary input apply to multi-output # ufuncs - array, _ = arrays_for_binary_ufunc + arrays, _ = arrays_for_binary_ufunc if sparse: - array = SparseArray(array) + arrays = SparseArray(arrays) - series = pd.Series(array, name="name") + series = pd.Series(arrays, name="name") result = np.modf(series) - expected = np.modf(array) + expected = np.modf(arrays) assert isinstance(result, tuple) assert isinstance(expected, tuple) From 91cba4263acad5a1e3c39803f827deccadcdfa22 Mon Sep 17 00:00:00 2001 From: deepang17 <47976918+deepang17@users.noreply.github.com> Date: Tue, 9 Mar 2021 01:35:39 +0530 Subject: [PATCH 08/14] updated test_interval.py --- pandas/tests/arithmetic/test_interval.py | 88 ++++++++++++------------ 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/pandas/tests/arithmetic/test_interval.py b/pandas/tests/arithmetic/test_interval.py index 0dd42f95cdeaf..d3b4f200f6b9f 100644 --- a/pandas/tests/arithmetic/test_interval.py +++ b/pandas/tests/arithmetic/test_interval.py @@ -50,7 +50,7 @@ def left_right_dtypes(request): @pytest.fixture -def array(left_right_dtypes): +def arrays(left_right_dtypes): """ Fixture to generate an IntervalArray of various dtypes containing NA if possible """ @@ -108,32 +108,32 @@ def elementwise_comparison(self, op, arrays, other): return Series(expected, index=other.index) return expected - def test_compare_scalar_interval(self, op, array): + def test_compare_scalar_interval(self, op, arrays): # matches first interval - other = array[0] - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + other = arrays[0] + result = op(arrays, other) + expected = self.elementwise_comparison(op, arrays, other) tm.assert_numpy_array_equal(result, expected) # matches on a single endpoint but not both - other = Interval(array.left[0], array.right[1]) - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + other = Interval(arrays.left[0], arrays.right[1]) + result = op(arrays, other) + expected = self.elementwise_comparison(op, arrays, other) tm.assert_numpy_array_equal(result, expected) def test_compare_scalar_interval_mixed_closed(self, op, closed, other_closed): - array = IntervalArray.from_arrays(range(2), range(1, 3), closed=closed) + arrays = IntervalArray.from_arrays(range(2), range(1, 3), closed=closed) other = Interval(0, 1, closed=other_closed) - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + result = op(arrays, other) + expected = self.elementwise_comparison(op, arrays, other) tm.assert_numpy_array_equal(result, expected) - def test_compare_scalar_na(self, op, array, nulls_fixture, request): - result = op(array, nulls_fixture) - expected = self.elementwise_comparison(op, array, nulls_fixture) + def test_compare_scalar_na(self, op, arrays, nulls_fixture, request): + result = op(arrays, nulls_fixture) + expected = self.elementwise_comparison(op, arrays, nulls_fixture) - if nulls_fixture is pd.NA and array.dtype.subtype != "int64": + if nulls_fixture is pd.NA and arrays.dtype.subtype != "int64": mark = pytest.mark.xfail( reason="broken for non-integer IntervalArray; see GH 31882" ) @@ -154,38 +154,38 @@ def test_compare_scalar_na(self, op, array, nulls_fixture, request): Period("2017-01-01", "D"), ], ) - def test_compare_scalar_other(self, op, array, other): - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + def test_compare_scalar_other(self, op, arrays, other): + result = op(arrays, other) + expected = self.elementwise_comparison(op, arrays, other) tm.assert_numpy_array_equal(result, expected) - def test_compare_list_like_interval(self, op, array, interval_constructor): + def test_compare_list_like_interval(self, op, arrays, interval_constructor): # same endpoints - other = interval_constructor(array.left, array.right) - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + other = interval_constructor(arrays.left, arrays.right) + result = op(arrays, other) + expected = self.elementwise_comparison(op, arrays, other) tm.assert_equal(result, expected) # different endpoints - other = interval_constructor(array.left[::-1], array.right[::-1]) - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + other = interval_constructor(arrays.left[::-1], arrays.right[::-1]) + result = op(arrays, other) + expected = self.elementwise_comparison(op, arrays, other) tm.assert_equal(result, expected) # all nan endpoints other = interval_constructor([np.nan] * 4, [np.nan] * 4) - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + result = op(arrays, other) + expected = self.elementwise_comparison(op, arrays, other) tm.assert_equal(result, expected) def test_compare_list_like_interval_mixed_closed( self, op, interval_constructor, closed, other_closed ): - array = IntervalArray.from_arrays(range(2), range(1, 3), closed=closed) + arrays = IntervalArray.from_arrays(range(2), range(1, 3), closed=closed) other = interval_constructor(range(2), range(1, 3), closed=other_closed) - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + result = op(arrays, other) + expected = self.elementwise_comparison(op, arrays, other) tm.assert_equal(result, expected) @pytest.mark.parametrize( @@ -206,17 +206,17 @@ def test_compare_list_like_interval_mixed_closed( ), ], ) - def test_compare_list_like_object(self, op, array, other): - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + def test_compare_list_like_object(self, op, arrays, other): + result = op(arrays, other) + expected = self.elementwise_comparison(op, arrays, other) tm.assert_numpy_array_equal(result, expected) - def test_compare_list_like_nan(self, op, array, nulls_fixture, request): + def test_compare_list_like_nan(self, op, arrays, nulls_fixture, request): other = [nulls_fixture] * 4 - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + result = op(arrays, other) + expected = self.elementwise_comparison(op, arrays, other) - if nulls_fixture is pd.NA and array.dtype.subtype != "i8": + if nulls_fixture is pd.NA and arrays.dtype.subtype != "i8": reason = "broken for non-integer IntervalArray; see GH 31882" mark = pytest.mark.xfail(reason=reason) request.node.add_marker(mark) @@ -234,23 +234,23 @@ def test_compare_list_like_nan(self, op, array, nulls_fixture, request): period_range("2017-01-01", periods=4, freq="D"), Categorical(list("abab")), Categorical(date_range("2017-01-01", periods=4)), - array(list("abcd")), - array(["foo", 3.14, None, object()]), + arrays(list("abcd")), + arrays(["foo", 3.14, None, object()]), ], ids=lambda x: str(x.dtype), ) - def test_compare_list_like_other(self, op, array, other): - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + def test_compare_list_like_other(self, op, arrays, other): + result = op(arrays, other) + expected = self.elementwise_comparison(op, arrays, other) tm.assert_numpy_array_equal(result, expected) @pytest.mark.parametrize("length", [1, 3, 5]) @pytest.mark.parametrize("other_constructor", [IntervalArray, list]) def test_compare_length_mismatch_errors(self, op, other_constructor, length): - array = IntervalArray.from_arrays(range(4), range(1, 5)) + arrays = IntervalArray.from_arrays(range(4), range(1, 5)) other = other_constructor([Interval(0, 1)] * length) with pytest.raises(ValueError, match="Lengths must match to compare"): - op(array, other) + op(arrays, other) @pytest.mark.parametrize( "constructor, expected_type, assert_func", From 61108f774e884f8dbf0cd2ee0add2c7968e3ed51 Mon Sep 17 00:00:00 2001 From: deepang17 <47976918+deepang17@users.noreply.github.com> Date: Tue, 9 Mar 2021 03:44:06 +0530 Subject: [PATCH 09/14] FIX: Improved varible names --- pandas/tests/arithmetic/test_interval.py | 94 ++++++++++----------- pandas/tests/arrays/sparse/test_array.py | 6 +- pandas/tests/arrays/string_/test_string.py | 16 ++-- pandas/tests/arrays/test_datetimelike.py | 34 ++++---- pandas/tests/base/test_conversion.py | 34 ++++---- pandas/tests/dtypes/test_inference.py | 6 +- pandas/tests/extension/base/getitem.py | 10 +-- pandas/tests/indexing/test_check_indexer.py | 20 ++--- pandas/tests/series/test_ufunc.py | 26 +++--- 9 files changed, 123 insertions(+), 123 deletions(-) diff --git a/pandas/tests/arithmetic/test_interval.py b/pandas/tests/arithmetic/test_interval.py index d3b4f200f6b9f..2303cbab57622 100644 --- a/pandas/tests/arithmetic/test_interval.py +++ b/pandas/tests/arithmetic/test_interval.py @@ -50,7 +50,7 @@ def left_right_dtypes(request): @pytest.fixture -def arrays(left_right_dtypes): +def interval_array(left_right_dtypes): """ Fixture to generate an IntervalArray of various dtypes containing NA if possible """ @@ -98,42 +98,42 @@ def interval_constructor(self, request): """ return request.param - def elementwise_comparison(self, op, arrays, other): + def elementwise_comparison(self, op, arr, other): """ Helper that performs elementwise comparisons between `array` and `other` """ - other = other if is_list_like(other) else [other] * len(arrays) - expected = np.array([op(x, y) for x, y in zip(arrays, other)]) + other = other if is_list_like(other) else [other] * len(arr) + expected = np.array([op(x, y) for x, y in zip(arr, other)]) if isinstance(other, Series): return Series(expected, index=other.index) return expected - def test_compare_scalar_interval(self, op, arrays): + def test_compare_scalar_interval(self, op, arr): # matches first interval - other = arrays[0] - result = op(arrays, other) - expected = self.elementwise_comparison(op, arrays, other) + other = arr[0] + result = op(arr, other) + expected = self.elementwise_comparison(op, arr, other) tm.assert_numpy_array_equal(result, expected) # matches on a single endpoint but not both - other = Interval(arrays.left[0], arrays.right[1]) - result = op(arrays, other) - expected = self.elementwise_comparison(op, arrays, other) + other = Interval(arr.left[0], arr.right[1]) + result = op(arr, other) + expected = self.elementwise_comparison(op, arr, other) tm.assert_numpy_array_equal(result, expected) def test_compare_scalar_interval_mixed_closed(self, op, closed, other_closed): - arrays = IntervalArray.from_arrays(range(2), range(1, 3), closed=closed) + arr = IntervalArray.from_arrays(range(2), range(1, 3), closed=closed) other = Interval(0, 1, closed=other_closed) - result = op(arrays, other) - expected = self.elementwise_comparison(op, arrays, other) + result = op(arr, other) + expected = self.elementwise_comparison(op, arr, other) tm.assert_numpy_array_equal(result, expected) - def test_compare_scalar_na(self, op, arrays, nulls_fixture, request): - result = op(arrays, nulls_fixture) - expected = self.elementwise_comparison(op, arrays, nulls_fixture) + def test_compare_scalar_na(self, op, arr, nulls_fixture, request): + result = op(arr, nulls_fixture) + expected = self.elementwise_comparison(op, arr, nulls_fixture) - if nulls_fixture is pd.NA and arrays.dtype.subtype != "int64": + if nulls_fixture is pd.NA and arr.dtype.subtype != "int64": mark = pytest.mark.xfail( reason="broken for non-integer IntervalArray; see GH 31882" ) @@ -154,38 +154,38 @@ def test_compare_scalar_na(self, op, arrays, nulls_fixture, request): Period("2017-01-01", "D"), ], ) - def test_compare_scalar_other(self, op, arrays, other): - result = op(arrays, other) - expected = self.elementwise_comparison(op, arrays, other) + def test_compare_scalar_other(self, op, arr, other): + result = op(arr, other) + expected = self.elementwise_comparison(op, arr, other) tm.assert_numpy_array_equal(result, expected) - def test_compare_list_like_interval(self, op, arrays, interval_constructor): + def test_compare_list_like_interval(self, op, arr, interval_constructor): # same endpoints - other = interval_constructor(arrays.left, arrays.right) - result = op(arrays, other) - expected = self.elementwise_comparison(op, arrays, other) + other = interval_constructor(arr.left, arr.right) + result = op(arr, other) + expected = self.elementwise_comparison(op, arr, other) tm.assert_equal(result, expected) # different endpoints - other = interval_constructor(arrays.left[::-1], arrays.right[::-1]) - result = op(arrays, other) - expected = self.elementwise_comparison(op, arrays, other) + other = interval_constructor(arr.left[::-1], arr.right[::-1]) + result = op(arr, other) + expected = self.elementwise_comparison(op, arr, other) tm.assert_equal(result, expected) # all nan endpoints other = interval_constructor([np.nan] * 4, [np.nan] * 4) - result = op(arrays, other) - expected = self.elementwise_comparison(op, arrays, other) + result = op(arr, other) + expected = self.elementwise_comparison(op, arr, other) tm.assert_equal(result, expected) def test_compare_list_like_interval_mixed_closed( self, op, interval_constructor, closed, other_closed ): - arrays = IntervalArray.from_arrays(range(2), range(1, 3), closed=closed) + arr = IntervalArray.from_arrays(range(2), range(1, 3), closed=closed) other = interval_constructor(range(2), range(1, 3), closed=other_closed) - result = op(arrays, other) - expected = self.elementwise_comparison(op, arrays, other) + result = op(arr, other) + expected = self.elementwise_comparison(op, arr, other) tm.assert_equal(result, expected) @pytest.mark.parametrize( @@ -206,17 +206,17 @@ def test_compare_list_like_interval_mixed_closed( ), ], ) - def test_compare_list_like_object(self, op, arrays, other): - result = op(arrays, other) - expected = self.elementwise_comparison(op, arrays, other) + def test_compare_list_like_object(self, op, arr, other): + result = op(arr, other) + expected = self.elementwise_comparison(op, arr, other) tm.assert_numpy_array_equal(result, expected) - def test_compare_list_like_nan(self, op, arrays, nulls_fixture, request): + def test_compare_list_like_nan(self, op, arr, nulls_fixture, request): other = [nulls_fixture] * 4 - result = op(arrays, other) - expected = self.elementwise_comparison(op, arrays, other) + result = op(arr, other) + expected = self.elementwise_comparison(op, arr, other) - if nulls_fixture is pd.NA and arrays.dtype.subtype != "i8": + if nulls_fixture is pd.NA and arr.dtype.subtype != "i8": reason = "broken for non-integer IntervalArray; see GH 31882" mark = pytest.mark.xfail(reason=reason) request.node.add_marker(mark) @@ -234,23 +234,23 @@ def test_compare_list_like_nan(self, op, arrays, nulls_fixture, request): period_range("2017-01-01", periods=4, freq="D"), Categorical(list("abab")), Categorical(date_range("2017-01-01", periods=4)), - arrays(list("abcd")), - arrays(["foo", 3.14, None, object()]), + pd.array(list("abcd")), + pd.array(["foo", 3.14, None, object()]), ], ids=lambda x: str(x.dtype), ) - def test_compare_list_like_other(self, op, arrays, other): - result = op(arrays, other) - expected = self.elementwise_comparison(op, arrays, other) + def test_compare_list_like_other(self, op, arr, other): + result = op(arr, other) + expected = self.elementwise_comparison(op, arr, other) tm.assert_numpy_array_equal(result, expected) @pytest.mark.parametrize("length", [1, 3, 5]) @pytest.mark.parametrize("other_constructor", [IntervalArray, list]) def test_compare_length_mismatch_errors(self, op, other_constructor, length): - arrays = IntervalArray.from_arrays(range(4), range(1, 5)) + arr = IntervalArray.from_arrays(range(4), range(1, 5)) other = other_constructor([Interval(0, 1)] * length) with pytest.raises(ValueError, match="Lengths must match to compare"): - op(arrays, other) + op(arr, other) @pytest.mark.parametrize( "constructor, expected_type, assert_func", diff --git a/pandas/tests/arrays/sparse/test_array.py b/pandas/tests/arrays/sparse/test_array.py index 9a0192b4b7cc1..21a11c665e8bc 100644 --- a/pandas/tests/arrays/sparse/test_array.py +++ b/pandas/tests/arrays/sparse/test_array.py @@ -522,7 +522,7 @@ def test_astype_all(self, any_real_dtype): tm.assert_numpy_array_equal(np.asarray(res.to_dense()), vals.astype(typ)) @pytest.mark.parametrize( - "arrays, dtype, expected", + "sparse_array, dtype, expected", [ ( SparseArray([0, 1]), @@ -557,8 +557,8 @@ def test_astype_all(self, any_real_dtype): ), ], ) - def test_astype_more(self, arrays, dtype, expected): - result = arrays.astype(dtype) + def test_astype_more(self, sparse_array, dtype, expected): + result = sparse_array.astype(dtype) tm.assert_sp_array_equal(result, expected) def test_astype_nan_raises(self): diff --git a/pandas/tests/arrays/string_/test_string.py b/pandas/tests/arrays/string_/test_string.py index 7ad27b609b0d0..df5330a015ac7 100644 --- a/pandas/tests/arrays/string_/test_string.py +++ b/pandas/tests/arrays/string_/test_string.py @@ -223,31 +223,31 @@ def test_mul(dtype, request): @pytest.mark.xfail(reason="GH-28527") def test_add_strings(dtype): - arrs = pd.array(["a", "b", "c", "d"], dtype=dtype) + pd_array = pd.array(["a", "b", "c", "d"], dtype=dtype) df = pd.DataFrame([["t", "u", "v", "w"]]) - assert arrs.__add__(df) is NotImplemented + assert pd_array.__add__(df) is NotImplemented - result = arrs + df + result = pd_array + df expected = pd.DataFrame([["at", "bu", "cv", "dw"]]).astype(dtype) tm.assert_frame_equal(result, expected) - result = df + arrs + result = df + pd_array expected = pd.DataFrame([["ta", "ub", "vc", "wd"]]).astype(dtype) tm.assert_frame_equal(result, expected) @pytest.mark.xfail(reason="GH-28527") def test_add_frame(dtype): - arrs = pd.array(["a", "b", np.nan, np.nan], dtype=dtype) + pd_array = pd.array(["a", "b", np.nan, np.nan], dtype=dtype) df = pd.DataFrame([["x", np.nan, "y", np.nan]]) - assert arrs.__add__(df) is NotImplemented + assert pd_array.__add__(df) is NotImplemented - result = arrs + df + result = pd_array + df expected = pd.DataFrame([["ax", np.nan, np.nan, np.nan]]).astype(dtype) tm.assert_frame_equal(result, expected) - result = df + arrs + result = df + pd_array expected = pd.DataFrame([["xa", np.nan, np.nan, np.nan]]).astype(dtype) tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index 65bd5abe10451..1e271432b2621 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -1160,7 +1160,7 @@ def test_strftime_nat(self): @pytest.mark.parametrize( - "arrays,casting_nats", + "ts_array,casting_nats", [ ( TimedeltaIndex(["1 Day", "3 Hours", "NaT"])._data, @@ -1174,17 +1174,17 @@ def test_strftime_nat(self): ], ids=lambda x: type(x).__name__, ) -def test_casting_nat_setitem_array(arrays, casting_nats): - expected = type(arrays)._from_sequence([NaT, arrays[1], arrays[2]]) +def test_casting_nat_setitem_array(ts_array, casting_nats): + expected = type(ts_array)._from_sequence([NaT, ts_array[1], ts_array[2]]) for nat in casting_nats: - arr = arrays.copy() + arr = ts_array.copy() arr[0] = nat tm.assert_equal(arr, expected) @pytest.mark.parametrize( - "arrays,non_casting_nats", + "ts_array,non_casting_nats", [ ( TimedeltaIndex(["1 Day", "3 Hours", "NaT"])._data, @@ -1201,7 +1201,7 @@ def test_casting_nat_setitem_array(arrays, casting_nats): ], ids=lambda x: type(x).__name__, ) -def test_invalid_nat_setitem_array(arrays, non_casting_nats): +def test_invalid_nat_setitem_array(ts_array, non_casting_nats): msg = ( "value should be a '(Timestamp|Timedelta|Period)', 'NaT', or array of those. " "Got '(timedelta64|datetime64|int)' instead." @@ -1209,42 +1209,42 @@ def test_invalid_nat_setitem_array(arrays, non_casting_nats): for nat in non_casting_nats: with pytest.raises(TypeError, match=msg): - arrays[0] = nat + ts_array[0] = nat @pytest.mark.parametrize( - "arrays", + "ts_array", [ pd.date_range("2000", periods=4).array, pd.timedelta_range("2000", periods=4).array, ], ) -def test_to_numpy_extra(arrays): +def test_to_numpy_extra(ts_array): if np_version_under1p18: # np.isnan(NaT) raises, so use pandas' isnan = pd.isna else: isnan = np.isnan - arrays[0] = NaT - original = arrays.copy() + ts_array[0] = NaT + original = ts_array.copy() - result = arrays.to_numpy() + result = ts_array.to_numpy() assert isnan(result[0]) - result = arrays.to_numpy(dtype="int64") + result = ts_array.to_numpy(dtype="int64") assert result[0] == -9223372036854775808 - result = arrays.to_numpy(dtype="int64", na_value=0) + result = ts_array.to_numpy(dtype="int64", na_value=0) assert result[0] == 0 - result = arrays.to_numpy(na_value=arrays[1].to_numpy()) + result = ts_array.to_numpy(na_value=ts_array[1].to_numpy()) assert result[0] == result[1] - result = arrays.to_numpy(na_value=arrays[1].to_numpy(copy=False)) + result = ts_array.to_numpy(na_value=ts_array[1].to_numpy(copy=False)) assert result[0] == result[1] - tm.assert_equal(arrays, original) + tm.assert_equal(ts_array, original) @pytest.mark.parametrize("as_index", [True, False]) diff --git a/pandas/tests/base/test_conversion.py b/pandas/tests/base/test_conversion.py index 9d7a5195fa099..54973346ccbaa 100644 --- a/pandas/tests/base/test_conversion.py +++ b/pandas/tests/base/test_conversion.py @@ -177,7 +177,7 @@ def test_iter_box(self): @pytest.mark.parametrize( - "arrays, expected_type, dtype", + "test_arr, expected_type, dtype", [ (np.array([0, 1], dtype=np.int64), np.ndarray, "int64"), (np.array(["a", "b"]), np.ndarray, "object"), @@ -216,9 +216,9 @@ def test_iter_box(self): ), ], ) -def test_values_consistent(arrays, expected_type, dtype): - l_values = Series(arrays)._values - r_values = pd.Index(arrays)._values +def test_values_consistent(test_arr, expected_type, dtype): + l_values = Series(test_arr)._values + r_values = pd.Index(test_arr)._values assert type(l_values) is expected_type assert type(l_values) is type(r_values) @@ -245,7 +245,7 @@ def test_numpy_array_all_dtypes(any_numpy_dtype): @pytest.mark.parametrize( - "arrays, attr", + "test_arr, attr", [ (pd.Categorical(["a", "b"]), "_codes"), (pd.core.arrays.period_array(["2000", "2001"], freq="D"), "_data"), @@ -265,17 +265,17 @@ def test_numpy_array_all_dtypes(any_numpy_dtype): ), ], ) -def test_array(arrays, attr, index_or_series): +def test_array(test_arr, attr, index_or_series): box = index_or_series - if arrays.dtype.name in ("Int64", "Sparse[int64, 0]") and box is pd.Index: - pytest.skip(f"No index type for {arrays.dtype}") - result = box(arrays, copy=False).arrays + if test_arr.dtype.name in ("Int64", "Sparse[int64, 0]") and box is pd.Index: + pytest.skip(f"No index type for {test_arr.dtype}") + result = box(test_arr, copy=False).array if attr: - arrays = getattr(arrays, attr) + test_arr = getattr(test_arr, attr) result = getattr(result, attr) - assert result is arrays + assert result is test_arr def test_array_multiindex_raises(): @@ -286,7 +286,7 @@ def test_array_multiindex_raises(): @pytest.mark.parametrize( - "arrays, expected", + "test_arr, expected", [ (np.array([1, 2], dtype=np.int64), np.array([1, 2], dtype=np.int64)), (pd.Categorical(["a", "b"]), np.array(["a", "b"], dtype=object)), @@ -337,14 +337,14 @@ def test_array_multiindex_raises(): ), ], ) -def test_to_numpy(arrays, expected, index_or_series_or_array, request): +def test_to_numpy(test_arr, expected, index_or_series_or_array, request): box = index_or_series_or_array - thing = box(arrays) + thing = box(test_arr) - if arrays.dtype.name in ("Int64", "Sparse[int64, 0]") and box is pd.Index: - pytest.skip(f"No index type for {arrays.dtype}") + if test_arr.dtype.name in ("Int64", "Sparse[int64, 0]") and box is pd.Index: + pytest.skip(f"No index type for {test_arr.dtype}") - if arrays.dtype.name == "int64" and box is pd.array: + if test_arr.dtype.name == "int64" and box is pd.array: mark = pytest.mark.xfail(reason="thing is Int64 and to_numpy() returns object") request.node.add_marker(mark) diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index 37a990c2dab65..9f7966423188b 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -616,9 +616,9 @@ def test_maybe_convert_objects_bool_nan(self): def test_mixed_dtypes_remain_object_array(self): # GH14956 - arrays = np.array([datetime(2015, 1, 1, tzinfo=pytz.utc), 1], dtype=object) - result = lib.maybe_convert_objects(arrays, convert_datetime=True) - tm.assert_numpy_array_equal(result, arrays) + np_array = np.array([datetime(2015, 1, 1, tzinfo=pytz.utc), 1], dtype=object) + result = lib.maybe_convert_objects(np_array, convert_datetime=True) + tm.assert_numpy_array_equal(result, np_array) class TestTypeInference: diff --git a/pandas/tests/extension/base/getitem.py b/pandas/tests/extension/base/getitem.py index ebd5bb7fb113c..db3e67589bd6f 100644 --- a/pandas/tests/extension/base/getitem.py +++ b/pandas/tests/extension/base/getitem.py @@ -324,11 +324,11 @@ def test_take_non_na_fill_value(self, data_missing): fill_value = data_missing[1] # valid na = data_missing[0] - arrays = data_missing._from_sequence( + d_arr = data_missing._from_sequence( [na, fill_value, na], dtype=data_missing.dtype ) - result = arrays.take([-1, 1], fill_value=fill_value, allow_fill=True) - expected = arrays.take([1, 1]) + result = d_arr.take([-1, 1], fill_value=fill_value, allow_fill=True) + expected = d_arr.take([1, 1]) self.assert_extension_array_equal(result, expected) def test_take_pandas_style_negative_raises(self, data, na_value): @@ -375,8 +375,8 @@ def test_reindex_non_na_fill_value(self, data_missing): valid = data_missing[1] na = data_missing[0] - arrays = data_missing._from_sequence([na, valid], dtype=data_missing.dtype) - ser = pd.Series(arrays) + d_arr = data_missing._from_sequence([na, valid], dtype=data_missing.dtype) + ser = pd.Series(d_arr) result = ser.reindex([0, 1, 2], fill_value=valid) expected = pd.Series( data_missing._from_sequence([na, valid, valid], dtype=data_missing.dtype) diff --git a/pandas/tests/indexing/test_check_indexer.py b/pandas/tests/indexing/test_check_indexer.py index 24a7159bc5974..f5f2c8b9cdfd8 100644 --- a/pandas/tests/indexing/test_check_indexer.py +++ b/pandas/tests/indexing/test_check_indexer.py @@ -26,8 +26,8 @@ ], ) def test_valid_input(indexer, expected): - arrays = np.array([1, 2, 3]) - result = check_array_indexer(arrays, indexer) + n_arr = np.array([1, 2, 3]) + result = check_array_indexer(n_arr, indexer) tm.assert_numpy_array_equal(result, expected) @@ -53,22 +53,22 @@ def test_boolean_na_returns_indexer(indexer): ], ) def test_bool_raise_length(indexer): - arrays = np.array([1, 2, 3]) + n_arr = np.array([1, 2, 3]) msg = "Boolean index has wrong length" with pytest.raises(IndexError, match=msg): - check_array_indexer(arrays, indexer) + check_array_indexer(n_arr, indexer) @pytest.mark.parametrize( "indexer", [[0, 1, None], pd.array([0, 1, pd.NA], dtype="Int64")] ) def test_int_raise_missing_values(indexer): - arrays = np.array([1, 2, 3]) + n_arr = np.array([1, 2, 3]) msg = "Cannot index with an integer indexer containing NA values" with pytest.raises(ValueError, match=msg): - check_array_indexer(arrays, indexer) + check_array_indexer(n_arr, indexer) @pytest.mark.parametrize( @@ -82,16 +82,16 @@ def test_int_raise_missing_values(indexer): ], ) def test_raise_invalid_array_dtypes(indexer): - arrays = np.array([1, 2, 3]) + n_arr = np.array([1, 2, 3]) msg = "arrays used as indices must be of integer or boolean type" with pytest.raises(IndexError, match=msg): - check_array_indexer(arrays, indexer) + check_array_indexer(n_arr, indexer) @pytest.mark.parametrize("indexer", [None, Ellipsis, slice(0, 3), (None,)]) def test_pass_through_non_array_likes(indexer): - arrays = np.array([1, 2, 3]) + n_arr = np.array([1, 2, 3]) - result = check_array_indexer(arrays, indexer) + result = check_array_indexer(n_arr, indexer) assert result == indexer diff --git a/pandas/tests/series/test_ufunc.py b/pandas/tests/series/test_ufunc.py index 19102f3098c6b..bc30cb4612dc7 100644 --- a/pandas/tests/series/test_ufunc.py +++ b/pandas/tests/series/test_ufunc.py @@ -31,17 +31,17 @@ def arrays_for_binary_ufunc(): @pytest.mark.parametrize("sparse", SPARSE, ids=SPARSE_IDS) def test_unary_ufunc(ufunc, sparse): # Test that ufunc(pd.Series) == pd.Series(ufunc) - arrays = np.random.randint(0, 10, 10, dtype="int64") - arrays[::2] = 0 + u_arr = np.random.randint(0, 10, 10, dtype="int64") + u_arr[::2] = 0 if sparse: - arrays = SparseArray(arrays, dtype=pd.SparseDtype("int64", 0)) + u_arr = SparseArray(u_arr, dtype=pd.SparseDtype("int64", 0)) index = list(string.ascii_letters[:10]) name = "name" - series = pd.Series(arrays, index=index, name=name) + series = pd.Series(u_arr, index=index, name=name) result = ufunc(series) - expected = pd.Series(ufunc(arrays), index=index, name=name) + expected = pd.Series(ufunc(u_arr), index=index, name=name) tm.assert_series_equal(result, expected) @@ -148,14 +148,14 @@ def test_binary_ufunc_scalar(ufunc, sparse, flip, arrays_for_binary_ufunc): # Test that # * ufunc(pd.Series, scalar) == pd.Series(ufunc(array, scalar)) # * ufunc(pd.Series, scalar) == ufunc(scalar, pd.Series) - arrays, _ = arrays_for_binary_ufunc + u_arr, _ = arrays_for_binary_ufunc if sparse: - arrays = SparseArray(arrays) + u_arr = SparseArray(u_arr) other = 2 - series = pd.Series(arrays, name="name") + series = pd.Series(u_arr, name="name") series_args = (series, other) - array_args = (arrays, other) + array_args = (u_arr, other) if flip: series_args = tuple(reversed(series_args)) @@ -207,14 +207,14 @@ def test_multiple_output_binary_ufuncs(ufunc, sparse, shuffle, arrays_for_binary def test_multiple_output_ufunc(sparse, arrays_for_binary_ufunc): # Test that the same conditions from unary input apply to multi-output # ufuncs - arrays, _ = arrays_for_binary_ufunc + u_arr, _ = arrays_for_binary_ufunc if sparse: - arrays = SparseArray(arrays) + u_arr = SparseArray(u_arr) - series = pd.Series(arrays, name="name") + series = pd.Series(u_arr, name="name") result = np.modf(series) - expected = np.modf(arrays) + expected = np.modf(u_arr) assert isinstance(result, tuple) assert isinstance(expected, tuple) From 23de5ef3da494582bd054a1d84b470f1e213994a Mon Sep 17 00:00:00 2001 From: deepang17 <47976918+deepang17@users.noreply.github.com> Date: Tue, 9 Mar 2021 04:15:04 +0530 Subject: [PATCH 10/14] test_interval.py fixes --- pandas/tests/arithmetic/test_interval.py | 90 ++++++++++++------------ 1 file changed, 46 insertions(+), 44 deletions(-) diff --git a/pandas/tests/arithmetic/test_interval.py b/pandas/tests/arithmetic/test_interval.py index 2303cbab57622..f1815b3e05367 100644 --- a/pandas/tests/arithmetic/test_interval.py +++ b/pandas/tests/arithmetic/test_interval.py @@ -98,42 +98,42 @@ def interval_constructor(self, request): """ return request.param - def elementwise_comparison(self, op, arr, other): + def elementwise_comparison(self, op, interval_array, other): """ Helper that performs elementwise comparisons between `array` and `other` """ - other = other if is_list_like(other) else [other] * len(arr) - expected = np.array([op(x, y) for x, y in zip(arr, other)]) + other = other if is_list_like(other) else [other] * len(interval_array) + expected = np.array([op(x, y) for x, y in zip(interval_array, other)]) if isinstance(other, Series): return Series(expected, index=other.index) return expected - def test_compare_scalar_interval(self, op, arr): + def test_compare_scalar_interval(self, op, interval_array): # matches first interval - other = arr[0] - result = op(arr, other) - expected = self.elementwise_comparison(op, arr, other) + other = interval_array[0] + result = op(interval_array, other) + expected = self.elementwise_comparison(op, interval_array, other) tm.assert_numpy_array_equal(result, expected) # matches on a single endpoint but not both - other = Interval(arr.left[0], arr.right[1]) - result = op(arr, other) - expected = self.elementwise_comparison(op, arr, other) + other = Interval(interval_array.left[0], interval_array.right[1]) + result = op(interval_array, other) + expected = self.elementwise_comparison(op, interval_array, other) tm.assert_numpy_array_equal(result, expected) def test_compare_scalar_interval_mixed_closed(self, op, closed, other_closed): - arr = IntervalArray.from_arrays(range(2), range(1, 3), closed=closed) + interval_array = IntervalArray.from_arrays(range(2), range(1, 3), closed=closed) other = Interval(0, 1, closed=other_closed) - result = op(arr, other) - expected = self.elementwise_comparison(op, arr, other) + result = op(interval_array, other) + expected = self.elementwise_comparison(op, interval_array, other) tm.assert_numpy_array_equal(result, expected) - def test_compare_scalar_na(self, op, arr, nulls_fixture, request): - result = op(arr, nulls_fixture) - expected = self.elementwise_comparison(op, arr, nulls_fixture) + def test_compare_scalar_na(self, op, interval_array, nulls_fixture, request): + result = op(interval_array, nulls_fixture) + expected = self.elementwise_comparison(op, interval_array, nulls_fixture) - if nulls_fixture is pd.NA and arr.dtype.subtype != "int64": + if nulls_fixture is pd.NA and interval_array.dtype.subtype != "int64": mark = pytest.mark.xfail( reason="broken for non-integer IntervalArray; see GH 31882" ) @@ -154,38 +154,40 @@ def test_compare_scalar_na(self, op, arr, nulls_fixture, request): Period("2017-01-01", "D"), ], ) - def test_compare_scalar_other(self, op, arr, other): - result = op(arr, other) - expected = self.elementwise_comparison(op, arr, other) + def test_compare_scalar_other(self, op, interval_array, other): + result = op(interval_array, other) + expected = self.elementwise_comparison(op, interval_array, other) tm.assert_numpy_array_equal(result, expected) - def test_compare_list_like_interval(self, op, arr, interval_constructor): + def test_compare_list_like_interval(self, op, interval_array, interval_constructor): # same endpoints - other = interval_constructor(arr.left, arr.right) - result = op(arr, other) - expected = self.elementwise_comparison(op, arr, other) + other = interval_constructor(interval_array.left, interval_array.right) + result = op(interval_array, other) + expected = self.elementwise_comparison(op, interval_array, other) tm.assert_equal(result, expected) # different endpoints - other = interval_constructor(arr.left[::-1], arr.right[::-1]) - result = op(arr, other) - expected = self.elementwise_comparison(op, arr, other) + other = interval_constructor( + interval_array.left[::-1], interval_array.right[::-1] + ) + result = op(interval_array, other) + expected = self.elementwise_comparison(op, interval_array, other) tm.assert_equal(result, expected) # all nan endpoints other = interval_constructor([np.nan] * 4, [np.nan] * 4) - result = op(arr, other) - expected = self.elementwise_comparison(op, arr, other) + result = op(interval_array, other) + expected = self.elementwise_comparison(op, interval_array, other) tm.assert_equal(result, expected) def test_compare_list_like_interval_mixed_closed( self, op, interval_constructor, closed, other_closed ): - arr = IntervalArray.from_arrays(range(2), range(1, 3), closed=closed) + interval_array = IntervalArray.from_arrays(range(2), range(1, 3), closed=closed) other = interval_constructor(range(2), range(1, 3), closed=other_closed) - result = op(arr, other) - expected = self.elementwise_comparison(op, arr, other) + result = op(interval_array, other) + expected = self.elementwise_comparison(op, interval_array, other) tm.assert_equal(result, expected) @pytest.mark.parametrize( @@ -206,17 +208,17 @@ def test_compare_list_like_interval_mixed_closed( ), ], ) - def test_compare_list_like_object(self, op, arr, other): - result = op(arr, other) - expected = self.elementwise_comparison(op, arr, other) + def test_compare_list_like_object(self, op, interval_array, other): + result = op(interval_array, other) + expected = self.elementwise_comparison(op, interval_array, other) tm.assert_numpy_array_equal(result, expected) - def test_compare_list_like_nan(self, op, arr, nulls_fixture, request): + def test_compare_list_like_nan(self, op, interval_array, nulls_fixture, request): other = [nulls_fixture] * 4 - result = op(arr, other) - expected = self.elementwise_comparison(op, arr, other) + result = op(interval_array, other) + expected = self.elementwise_comparison(op, interval_array, other) - if nulls_fixture is pd.NA and arr.dtype.subtype != "i8": + if nulls_fixture is pd.NA and interval_array.dtype.subtype != "i8": reason = "broken for non-integer IntervalArray; see GH 31882" mark = pytest.mark.xfail(reason=reason) request.node.add_marker(mark) @@ -239,18 +241,18 @@ def test_compare_list_like_nan(self, op, arr, nulls_fixture, request): ], ids=lambda x: str(x.dtype), ) - def test_compare_list_like_other(self, op, arr, other): - result = op(arr, other) - expected = self.elementwise_comparison(op, arr, other) + def test_compare_list_like_other(self, op, interval_array, other): + result = op(interval_array, other) + expected = self.elementwise_comparison(op, interval_array, other) tm.assert_numpy_array_equal(result, expected) @pytest.mark.parametrize("length", [1, 3, 5]) @pytest.mark.parametrize("other_constructor", [IntervalArray, list]) def test_compare_length_mismatch_errors(self, op, other_constructor, length): - arr = IntervalArray.from_arrays(range(4), range(1, 5)) + interval_array = IntervalArray.from_arrays(range(4), range(1, 5)) other = other_constructor([Interval(0, 1)] * length) with pytest.raises(ValueError, match="Lengths must match to compare"): - op(arr, other) + op(interval_array, other) @pytest.mark.parametrize( "constructor, expected_type, assert_func", From 373b4e474fe527938a8be7c5162eec4d801312ef Mon Sep 17 00:00:00 2001 From: deepang17 <47976918+deepang17@users.noreply.github.com> Date: Tue, 9 Mar 2021 16:06:28 +0530 Subject: [PATCH 11/14] FIX: Resolved confusing variable names --- pandas/tests/indexing/test_check_indexer.py | 20 ++++++++-------- pandas/tests/series/test_ufunc.py | 26 ++++++++++----------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/pandas/tests/indexing/test_check_indexer.py b/pandas/tests/indexing/test_check_indexer.py index f5f2c8b9cdfd8..0e52c075d5af5 100644 --- a/pandas/tests/indexing/test_check_indexer.py +++ b/pandas/tests/indexing/test_check_indexer.py @@ -26,8 +26,8 @@ ], ) def test_valid_input(indexer, expected): - n_arr = np.array([1, 2, 3]) - result = check_array_indexer(n_arr, indexer) + arr = np.array([1, 2, 3]) + result = check_array_indexer(arr, indexer) tm.assert_numpy_array_equal(result, expected) @@ -53,22 +53,22 @@ def test_boolean_na_returns_indexer(indexer): ], ) def test_bool_raise_length(indexer): - n_arr = np.array([1, 2, 3]) + arr = np.array([1, 2, 3]) msg = "Boolean index has wrong length" with pytest.raises(IndexError, match=msg): - check_array_indexer(n_arr, indexer) + check_array_indexer(arr, indexer) @pytest.mark.parametrize( "indexer", [[0, 1, None], pd.array([0, 1, pd.NA], dtype="Int64")] ) def test_int_raise_missing_values(indexer): - n_arr = np.array([1, 2, 3]) + arr = np.array([1, 2, 3]) msg = "Cannot index with an integer indexer containing NA values" with pytest.raises(ValueError, match=msg): - check_array_indexer(n_arr, indexer) + check_array_indexer(arr, indexer) @pytest.mark.parametrize( @@ -82,16 +82,16 @@ def test_int_raise_missing_values(indexer): ], ) def test_raise_invalid_array_dtypes(indexer): - n_arr = np.array([1, 2, 3]) + arr = np.array([1, 2, 3]) msg = "arrays used as indices must be of integer or boolean type" with pytest.raises(IndexError, match=msg): - check_array_indexer(n_arr, indexer) + check_array_indexer(arr, indexer) @pytest.mark.parametrize("indexer", [None, Ellipsis, slice(0, 3), (None,)]) def test_pass_through_non_array_likes(indexer): - n_arr = np.array([1, 2, 3]) + arr = np.array([1, 2, 3]) - result = check_array_indexer(n_arr, indexer) + result = check_array_indexer(arr, indexer) assert result == indexer diff --git a/pandas/tests/series/test_ufunc.py b/pandas/tests/series/test_ufunc.py index bc30cb4612dc7..15b2ff36cff1e 100644 --- a/pandas/tests/series/test_ufunc.py +++ b/pandas/tests/series/test_ufunc.py @@ -31,17 +31,17 @@ def arrays_for_binary_ufunc(): @pytest.mark.parametrize("sparse", SPARSE, ids=SPARSE_IDS) def test_unary_ufunc(ufunc, sparse): # Test that ufunc(pd.Series) == pd.Series(ufunc) - u_arr = np.random.randint(0, 10, 10, dtype="int64") - u_arr[::2] = 0 + arr = np.random.randint(0, 10, 10, dtype="int64") + arr[::2] = 0 if sparse: - u_arr = SparseArray(u_arr, dtype=pd.SparseDtype("int64", 0)) + arr = SparseArray(arr, dtype=pd.SparseDtype("int64", 0)) index = list(string.ascii_letters[:10]) name = "name" - series = pd.Series(u_arr, index=index, name=name) + series = pd.Series(arr, index=index, name=name) result = ufunc(series) - expected = pd.Series(ufunc(u_arr), index=index, name=name) + expected = pd.Series(ufunc(arr), index=index, name=name) tm.assert_series_equal(result, expected) @@ -148,14 +148,14 @@ def test_binary_ufunc_scalar(ufunc, sparse, flip, arrays_for_binary_ufunc): # Test that # * ufunc(pd.Series, scalar) == pd.Series(ufunc(array, scalar)) # * ufunc(pd.Series, scalar) == ufunc(scalar, pd.Series) - u_arr, _ = arrays_for_binary_ufunc + arr, _ = arrays_for_binary_ufunc if sparse: - u_arr = SparseArray(u_arr) + arr = SparseArray(arr) other = 2 - series = pd.Series(u_arr, name="name") + series = pd.Series(arr, name="name") series_args = (series, other) - array_args = (u_arr, other) + array_args = (arr, other) if flip: series_args = tuple(reversed(series_args)) @@ -207,14 +207,14 @@ def test_multiple_output_binary_ufuncs(ufunc, sparse, shuffle, arrays_for_binary def test_multiple_output_ufunc(sparse, arrays_for_binary_ufunc): # Test that the same conditions from unary input apply to multi-output # ufuncs - u_arr, _ = arrays_for_binary_ufunc + arr, _ = arrays_for_binary_ufunc if sparse: - u_arr = SparseArray(u_arr) + arr = SparseArray(arr) - series = pd.Series(u_arr, name="name") + series = pd.Series(arr, name="name") result = np.modf(series) - expected = np.modf(u_arr) + expected = np.modf(arr) assert isinstance(result, tuple) assert isinstance(expected, tuple) From 92c177b90e6016b76750f2b0933bf85a59456306 Mon Sep 17 00:00:00 2001 From: deepang17 <47976918+deepang17@users.noreply.github.com> Date: Tue, 9 Mar 2021 23:01:54 +0530 Subject: [PATCH 12/14] test_string.py fixes --- pandas/tests/arrays/string_/test_string.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/tests/arrays/string_/test_string.py b/pandas/tests/arrays/string_/test_string.py index df5330a015ac7..0574061a6a544 100644 --- a/pandas/tests/arrays/string_/test_string.py +++ b/pandas/tests/arrays/string_/test_string.py @@ -223,31 +223,31 @@ def test_mul(dtype, request): @pytest.mark.xfail(reason="GH-28527") def test_add_strings(dtype): - pd_array = pd.array(["a", "b", "c", "d"], dtype=dtype) + arr = pd.array(["a", "b", "c", "d"], dtype=dtype) df = pd.DataFrame([["t", "u", "v", "w"]]) - assert pd_array.__add__(df) is NotImplemented + assert arr.__add__(df) is NotImplemented - result = pd_array + df + result = arr + df expected = pd.DataFrame([["at", "bu", "cv", "dw"]]).astype(dtype) tm.assert_frame_equal(result, expected) - result = df + pd_array + result = df + arr expected = pd.DataFrame([["ta", "ub", "vc", "wd"]]).astype(dtype) tm.assert_frame_equal(result, expected) @pytest.mark.xfail(reason="GH-28527") def test_add_frame(dtype): - pd_array = pd.array(["a", "b", np.nan, np.nan], dtype=dtype) + arr = pd.array(["a", "b", np.nan, np.nan], dtype=dtype) df = pd.DataFrame([["x", np.nan, "y", np.nan]]) - assert pd_array.__add__(df) is NotImplemented + assert arr.__add__(df) is NotImplemented - result = pd_array + df + result = arr + df expected = pd.DataFrame([["ax", np.nan, np.nan, np.nan]]).astype(dtype) tm.assert_frame_equal(result, expected) - result = df + pd_array + result = df + arr expected = pd.DataFrame([["xa", np.nan, np.nan, np.nan]]).astype(dtype) tm.assert_frame_equal(result, expected) From a3ceb655e3b2e0e1f525088fae6eb48f950a5892 Mon Sep 17 00:00:00 2001 From: deepang17 <47976918+deepang17@users.noreply.github.com> Date: Wed, 10 Mar 2021 15:31:51 +0530 Subject: [PATCH 13/14] getitem.py fixes --- pandas/tests/extension/base/getitem.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/tests/extension/base/getitem.py b/pandas/tests/extension/base/getitem.py index db3e67589bd6f..a7b99c2e09e88 100644 --- a/pandas/tests/extension/base/getitem.py +++ b/pandas/tests/extension/base/getitem.py @@ -324,11 +324,11 @@ def test_take_non_na_fill_value(self, data_missing): fill_value = data_missing[1] # valid na = data_missing[0] - d_arr = data_missing._from_sequence( + arr = data_missing._from_sequence( [na, fill_value, na], dtype=data_missing.dtype ) - result = d_arr.take([-1, 1], fill_value=fill_value, allow_fill=True) - expected = d_arr.take([1, 1]) + result = arr.take([-1, 1], fill_value=fill_value, allow_fill=True) + expected = arr.take([1, 1]) self.assert_extension_array_equal(result, expected) def test_take_pandas_style_negative_raises(self, data, na_value): @@ -375,8 +375,8 @@ def test_reindex_non_na_fill_value(self, data_missing): valid = data_missing[1] na = data_missing[0] - d_arr = data_missing._from_sequence([na, valid], dtype=data_missing.dtype) - ser = pd.Series(d_arr) + arr = data_missing._from_sequence([na, valid], dtype=data_missing.dtype) + ser = pd.Series(arr) result = ser.reindex([0, 1, 2], fill_value=valid) expected = pd.Series( data_missing._from_sequence([na, valid, valid], dtype=data_missing.dtype) From 40a35eeb3bb2ca8dad39df833e5fb40658be45db Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Thu, 11 Mar 2021 10:53:59 +0000 Subject: [PATCH 14/14] rename more variables to arr --- pandas/tests/arrays/sparse/test_array.py | 6 ++--- pandas/tests/arrays/test_datetimelike.py | 34 ++++++++++++------------ pandas/tests/base/test_conversion.py | 34 ++++++++++++------------ pandas/tests/dtypes/test_inference.py | 6 ++--- 4 files changed, 40 insertions(+), 40 deletions(-) diff --git a/pandas/tests/arrays/sparse/test_array.py b/pandas/tests/arrays/sparse/test_array.py index 21a11c665e8bc..d656a1a7ccc74 100644 --- a/pandas/tests/arrays/sparse/test_array.py +++ b/pandas/tests/arrays/sparse/test_array.py @@ -522,7 +522,7 @@ def test_astype_all(self, any_real_dtype): tm.assert_numpy_array_equal(np.asarray(res.to_dense()), vals.astype(typ)) @pytest.mark.parametrize( - "sparse_array, dtype, expected", + "arr, dtype, expected", [ ( SparseArray([0, 1]), @@ -557,8 +557,8 @@ def test_astype_all(self, any_real_dtype): ), ], ) - def test_astype_more(self, sparse_array, dtype, expected): - result = sparse_array.astype(dtype) + def test_astype_more(self, arr, dtype, expected): + result = arr.astype(dtype) tm.assert_sp_array_equal(result, expected) def test_astype_nan_raises(self): diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index 1e271432b2621..3cb67b7a57a8e 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -1160,7 +1160,7 @@ def test_strftime_nat(self): @pytest.mark.parametrize( - "ts_array,casting_nats", + "arr,casting_nats", [ ( TimedeltaIndex(["1 Day", "3 Hours", "NaT"])._data, @@ -1174,17 +1174,17 @@ def test_strftime_nat(self): ], ids=lambda x: type(x).__name__, ) -def test_casting_nat_setitem_array(ts_array, casting_nats): - expected = type(ts_array)._from_sequence([NaT, ts_array[1], ts_array[2]]) +def test_casting_nat_setitem_array(arr, casting_nats): + expected = type(arr)._from_sequence([NaT, arr[1], arr[2]]) for nat in casting_nats: - arr = ts_array.copy() + arr = arr.copy() arr[0] = nat tm.assert_equal(arr, expected) @pytest.mark.parametrize( - "ts_array,non_casting_nats", + "arr,non_casting_nats", [ ( TimedeltaIndex(["1 Day", "3 Hours", "NaT"])._data, @@ -1201,7 +1201,7 @@ def test_casting_nat_setitem_array(ts_array, casting_nats): ], ids=lambda x: type(x).__name__, ) -def test_invalid_nat_setitem_array(ts_array, non_casting_nats): +def test_invalid_nat_setitem_array(arr, non_casting_nats): msg = ( "value should be a '(Timestamp|Timedelta|Period)', 'NaT', or array of those. " "Got '(timedelta64|datetime64|int)' instead." @@ -1209,42 +1209,42 @@ def test_invalid_nat_setitem_array(ts_array, non_casting_nats): for nat in non_casting_nats: with pytest.raises(TypeError, match=msg): - ts_array[0] = nat + arr[0] = nat @pytest.mark.parametrize( - "ts_array", + "arr", [ pd.date_range("2000", periods=4).array, pd.timedelta_range("2000", periods=4).array, ], ) -def test_to_numpy_extra(ts_array): +def test_to_numpy_extra(arr): if np_version_under1p18: # np.isnan(NaT) raises, so use pandas' isnan = pd.isna else: isnan = np.isnan - ts_array[0] = NaT - original = ts_array.copy() + arr[0] = NaT + original = arr.copy() - result = ts_array.to_numpy() + result = arr.to_numpy() assert isnan(result[0]) - result = ts_array.to_numpy(dtype="int64") + result = arr.to_numpy(dtype="int64") assert result[0] == -9223372036854775808 - result = ts_array.to_numpy(dtype="int64", na_value=0) + result = arr.to_numpy(dtype="int64", na_value=0) assert result[0] == 0 - result = ts_array.to_numpy(na_value=ts_array[1].to_numpy()) + result = arr.to_numpy(na_value=arr[1].to_numpy()) assert result[0] == result[1] - result = ts_array.to_numpy(na_value=ts_array[1].to_numpy(copy=False)) + result = arr.to_numpy(na_value=arr[1].to_numpy(copy=False)) assert result[0] == result[1] - tm.assert_equal(ts_array, original) + tm.assert_equal(arr, original) @pytest.mark.parametrize("as_index", [True, False]) diff --git a/pandas/tests/base/test_conversion.py b/pandas/tests/base/test_conversion.py index 54973346ccbaa..7045a0abbeb81 100644 --- a/pandas/tests/base/test_conversion.py +++ b/pandas/tests/base/test_conversion.py @@ -177,7 +177,7 @@ def test_iter_box(self): @pytest.mark.parametrize( - "test_arr, expected_type, dtype", + "arr, expected_type, dtype", [ (np.array([0, 1], dtype=np.int64), np.ndarray, "int64"), (np.array(["a", "b"]), np.ndarray, "object"), @@ -216,9 +216,9 @@ def test_iter_box(self): ), ], ) -def test_values_consistent(test_arr, expected_type, dtype): - l_values = Series(test_arr)._values - r_values = pd.Index(test_arr)._values +def test_values_consistent(arr, expected_type, dtype): + l_values = Series(arr)._values + r_values = pd.Index(arr)._values assert type(l_values) is expected_type assert type(l_values) is type(r_values) @@ -245,7 +245,7 @@ def test_numpy_array_all_dtypes(any_numpy_dtype): @pytest.mark.parametrize( - "test_arr, attr", + "arr, attr", [ (pd.Categorical(["a", "b"]), "_codes"), (pd.core.arrays.period_array(["2000", "2001"], freq="D"), "_data"), @@ -265,17 +265,17 @@ def test_numpy_array_all_dtypes(any_numpy_dtype): ), ], ) -def test_array(test_arr, attr, index_or_series): +def test_array(arr, attr, index_or_series): box = index_or_series - if test_arr.dtype.name in ("Int64", "Sparse[int64, 0]") and box is pd.Index: - pytest.skip(f"No index type for {test_arr.dtype}") - result = box(test_arr, copy=False).array + if arr.dtype.name in ("Int64", "Sparse[int64, 0]") and box is pd.Index: + pytest.skip(f"No index type for {arr.dtype}") + result = box(arr, copy=False).array if attr: - test_arr = getattr(test_arr, attr) + arr = getattr(arr, attr) result = getattr(result, attr) - assert result is test_arr + assert result is arr def test_array_multiindex_raises(): @@ -286,7 +286,7 @@ def test_array_multiindex_raises(): @pytest.mark.parametrize( - "test_arr, expected", + "arr, expected", [ (np.array([1, 2], dtype=np.int64), np.array([1, 2], dtype=np.int64)), (pd.Categorical(["a", "b"]), np.array(["a", "b"], dtype=object)), @@ -337,14 +337,14 @@ def test_array_multiindex_raises(): ), ], ) -def test_to_numpy(test_arr, expected, index_or_series_or_array, request): +def test_to_numpy(arr, expected, index_or_series_or_array, request): box = index_or_series_or_array - thing = box(test_arr) + thing = box(arr) - if test_arr.dtype.name in ("Int64", "Sparse[int64, 0]") and box is pd.Index: - pytest.skip(f"No index type for {test_arr.dtype}") + if arr.dtype.name in ("Int64", "Sparse[int64, 0]") and box is pd.Index: + pytest.skip(f"No index type for {arr.dtype}") - if test_arr.dtype.name == "int64" and box is pd.array: + if arr.dtype.name == "int64" and box is pd.array: mark = pytest.mark.xfail(reason="thing is Int64 and to_numpy() returns object") request.node.add_marker(mark) diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index 9f7966423188b..b3c6015475674 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -616,9 +616,9 @@ def test_maybe_convert_objects_bool_nan(self): def test_mixed_dtypes_remain_object_array(self): # GH14956 - np_array = np.array([datetime(2015, 1, 1, tzinfo=pytz.utc), 1], dtype=object) - result = lib.maybe_convert_objects(np_array, convert_datetime=True) - tm.assert_numpy_array_equal(result, np_array) + arr = np.array([datetime(2015, 1, 1, tzinfo=pytz.utc), 1], dtype=object) + result = lib.maybe_convert_objects(arr, convert_datetime=True) + tm.assert_numpy_array_equal(result, arr) class TestTypeInference: