Skip to content

Remove numpy compat functions #44895

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions pandas/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
from pandas._typing import F
from pandas.compat.numpy import (
is_numpy_dev,
np_array_datetime64_compat,
np_datetime64_compat,
np_version_under1p19,
np_version_under1p20,
)
Expand Down Expand Up @@ -130,8 +128,6 @@ def get_lzma_file():

__all__ = [
"is_numpy_dev",
"np_array_datetime64_compat",
"np_datetime64_compat",
"np_version_under1p19",
"np_version_under1p20",
"pa_version_under1p01",
Expand Down
41 changes: 0 additions & 41 deletions pandas/compat/numpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
""" support numpy compatibility across versions """

import re

import numpy as np

from pandas.util.version import Version
Expand Down Expand Up @@ -29,44 +26,6 @@
)


_tz_regex = re.compile("[+-]0000$")


def _tz_replacer(tstring):
if isinstance(tstring, str):
if tstring.endswith("Z"):
tstring = tstring[:-1]
elif _tz_regex.search(tstring):
tstring = tstring[:-5]
return tstring


def np_datetime64_compat(tstring: str, unit: str = "ns"):
"""
provide compat for construction of strings to numpy datetime64's with
tz-changes in 1.11 that make '2015-01-01 09:00:00Z' show a deprecation
warning, when need to pass '2015-01-01 09:00:00'
"""
tstring = _tz_replacer(tstring)
return np.datetime64(tstring, unit)


def np_array_datetime64_compat(arr, dtype="M8[ns]"):
"""
provide compat for construction of an array of strings to a
np.array(..., dtype=np.datetime64(..))
tz-changes in 1.11 that make '2015-01-01 09:00:00Z' show a deprecation
warning, when need to pass '2015-01-01 09:00:00'
"""
# is_list_like; can't import as it would be circular
if hasattr(arr, "__iter__") and not isinstance(arr, (str, bytes)):
arr = [_tz_replacer(s) for s in arr]
else:
arr = _tz_replacer(arr)

return np.array(arr, dtype=dtype)


__all__ = [
"np",
"_np_version",
Expand Down
11 changes: 5 additions & 6 deletions pandas/tests/arithmetic/test_datetime64.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

from pandas._libs.tslibs.conversion import localize_pydatetime
from pandas._libs.tslibs.offsets import shift_months
from pandas.compat import np_datetime64_compat
from pandas.errors import PerformanceWarning

import pandas as pd
Expand Down Expand Up @@ -487,12 +486,12 @@ def test_dti_cmp_nat_behaves_like_float_cmp_nan(self):
)
darr = np.array(
[
np_datetime64_compat("2014-02-01 00:00Z"),
np_datetime64_compat("2014-03-01 00:00Z"),
np_datetime64_compat("nat"),
np.datetime64("2014-02-01 00:00"),
np.datetime64("2014-03-01 00:00"),
np.datetime64("nat"),
np_datetime64_compat("2014-06-01 00:00Z"),
np_datetime64_compat("2014-07-01 00:00Z"),
np.datetime64("nat"),
np.datetime64("2014-06-01 00:00"),
np.datetime64("2014-07-01 00:00"),
]
)

Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/base/test_value_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import numpy as np
import pytest

from pandas.compat import np_array_datetime64_compat

import pandas as pd
from pandas import (
DatetimeIndex,
Expand Down Expand Up @@ -212,7 +210,7 @@ def test_value_counts_datetime64(index_or_series):
expected_s = Series([3, 2, 1], index=idx)
tm.assert_series_equal(s.value_counts(), expected_s)

expected = np_array_datetime64_compat(
expected = np.array(
["2010-01-01 00:00:00", "2009-01-01 00:00:00", "2008-09-09 00:00:00"],
dtype="datetime64[ns]",
)
Expand Down
15 changes: 2 additions & 13 deletions pandas/tests/indexes/test_index_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import numpy as np
import pytest

from pandas.compat import np_datetime64_compat

from pandas.core.dtypes.common import is_unsigned_integer_dtype

from pandas import (
Expand Down Expand Up @@ -273,9 +271,7 @@ def test_constructor_dtypes_to_float64(self, vals):
[
[1, 2, 3],
np.array([1, 2, 3], dtype=int),
np.array(
[np_datetime64_compat("2011-01-01"), np_datetime64_compat("2011-01-02")]
),
np.array(["2011-01-01", "2011-01-02"], dtype="datetime64[ns]"),
[datetime(2011, 1, 1), datetime(2011, 1, 2)],
],
)
Expand All @@ -287,14 +283,7 @@ def test_constructor_dtypes_to_categorical(self, vals):
@pytest.mark.parametrize(
"vals",
[
Index(
np.array(
[
np_datetime64_compat("2011-01-01"),
np_datetime64_compat("2011-01-02"),
]
)
),
Index(np.array([np.datetime64("2011-01-01"), np.datetime64("2011-01-02")])),
Index([datetime(2011, 1, 1), datetime(2011, 1, 2)]),
],
)
Expand Down
7 changes: 2 additions & 5 deletions pandas/tests/io/parser/test_parse_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

from pandas._libs.tslibs import parsing
from pandas._libs.tslibs.parsing import parse_datetime_string
from pandas.compat import np_array_datetime64_compat
from pandas.compat.pyarrow import pa_version_under6p0

import pandas as pd
Expand Down Expand Up @@ -1541,7 +1540,7 @@ def test_date_parser_resolution_if_not_ns(all_parsers):
"""

def date_parser(dt, time):
return np_array_datetime64_compat(dt + "T" + time + "Z", dtype="datetime64[s]")
return np.array(dt + "T" + time, dtype="datetime64[s]")

result = parser.read_csv(
StringIO(data),
Expand All @@ -1550,9 +1549,7 @@ def date_parser(dt, time):
index_col=["datetime", "prn"],
)

datetimes = np_array_datetime64_compat(
["2013-11-03T19:00:00Z"] * 3, dtype="datetime64[s]"
)
datetimes = np.array(["2013-11-03T19:00:00"] * 3, dtype="datetime64[s]")
expected = DataFrame(
data={"rxstatus": ["00E80000"] * 3},
index=MultiIndex.from_tuples(
Expand Down
26 changes: 7 additions & 19 deletions pandas/tests/plotting/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import pandas._config.config as cf

from pandas.compat import np_datetime64_compat
import pandas.util._test_decorators as td

from pandas import (
Expand Down Expand Up @@ -185,21 +184,14 @@ def test_conversion(self):
assert rs == xp

# also testing datetime64 dtype (GH8614)
rs = self.dtc.convert(np_datetime64_compat("2012-01-01"), None, None)
rs = self.dtc.convert("2012-01-01", None, None)
assert rs == xp

rs = self.dtc.convert(
np_datetime64_compat("2012-01-01 00:00:00+0000"), None, None
)
rs = self.dtc.convert("2012-01-01 00:00:00+0000", None, None)
assert rs == xp

rs = self.dtc.convert(
np.array(
[
np_datetime64_compat("2012-01-01 00:00:00+0000"),
np_datetime64_compat("2012-01-02 00:00:00+0000"),
]
),
np.array(["2012-01-01 00:00:00+0000", "2012-01-02 00:00:00+0000"]),
None,
None,
)
Expand Down Expand Up @@ -334,20 +326,16 @@ def test_conversion(self):
rs = self.pc.convert(Timestamp("2012-1-1"), None, self.axis)
assert rs == xp

rs = self.pc.convert(np_datetime64_compat("2012-01-01"), None, self.axis)
rs = self.pc.convert("2012-01-01", None, self.axis)
assert rs == xp

rs = self.pc.convert(
np_datetime64_compat("2012-01-01 00:00:00+0000"), None, self.axis
)
rs = self.pc.convert("2012-01-01 00:00:00+0000", None, self.axis)
assert rs == xp

rs = self.pc.convert(
np.array(
[
np_datetime64_compat("2012-01-01 00:00:00+0000"),
np_datetime64_compat("2012-01-02 00:00:00+0000"),
]
["2012-01-01 00:00:00+0000", "2012-01-02 00:00:00+0000"],
dtype="datetime64[ns]",
),
None,
self.axis,
Expand Down
17 changes: 8 additions & 9 deletions pandas/tests/scalar/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
dateutil_gettz,
maybe_get_tz,
)
from pandas.compat import np_datetime64_compat

import pandas as pd
from pandas import (
Expand Down Expand Up @@ -92,14 +91,14 @@ def test_construction(self):
expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1000), freq="L")
assert i1 == expected

expected = Period(np_datetime64_compat("2007-01-01 09:00:00.001Z"), freq="L")
expected = Period("2007-01-01 09:00:00.001", freq="L")
assert i1 == expected

i1 = Period("2007-01-01 09:00:00.00101")
expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1010), freq="U")
assert i1 == expected

expected = Period(np_datetime64_compat("2007-01-01 09:00:00.00101Z"), freq="U")
expected = Period("2007-01-01 09:00:00.00101", freq="U")
assert i1 == expected

msg = "Must supply freq for ordinal value"
Expand Down Expand Up @@ -190,8 +189,8 @@ def test_construction_month(self):
i1 = Period(date(2007, 1, 1), freq="M")
i2 = Period(datetime(2007, 1, 1), freq="M")
i3 = Period(np.datetime64("2007-01-01"), freq="M")
i4 = Period(np_datetime64_compat("2007-01-01 00:00:00Z"), freq="M")
i5 = Period(np_datetime64_compat("2007-01-01 00:00:00.000Z"), freq="M")
i4 = Period("2007-01-01 00:00:00", freq="M")
i5 = Period("2007-01-01 00:00:00.000", freq="M")
assert i1 == i2
assert i1 == i3
assert i1 == i4
Expand Down Expand Up @@ -245,8 +244,8 @@ def test_period_constructor_offsets(self):
i1 = Period(date(2007, 1, 1), freq="M")
i2 = Period(datetime(2007, 1, 1), freq="M")
i3 = Period(np.datetime64("2007-01-01"), freq="M")
i4 = Period(np_datetime64_compat("2007-01-01 00:00:00Z"), freq="M")
i5 = Period(np_datetime64_compat("2007-01-01 00:00:00.000Z"), freq="M")
i4 = Period("2007-01-01 00:00:00", freq="M")
i5 = Period("2007-01-01 00:00:00.000", freq="M")
assert i1 == i2
assert i1 == i3
assert i1 == i4
Expand All @@ -256,14 +255,14 @@ def test_period_constructor_offsets(self):
expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1000), freq="L")
assert i1 == expected

expected = Period(np_datetime64_compat("2007-01-01 09:00:00.001Z"), freq="L")
expected = Period("2007-01-01 09:00:00.001", freq="L")
assert i1 == expected

i1 = Period("2007-01-01 09:00:00.00101")
expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1010), freq="U")
assert i1 == expected

expected = Period(np_datetime64_compat("2007-01-01 09:00:00.00101Z"), freq="U")
expected = Period("2007-01-01 09:00:00.00101", freq="U")
assert i1 == expected

def test_invalid_arguments(self):
Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/scalar/timestamp/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
dateutil_gettz as gettz,
get_timezone,
)
from pandas.compat import np_datetime64_compat
import pandas.util._test_decorators as td

from pandas import (
Expand Down Expand Up @@ -492,7 +491,7 @@ def test_nanosecond_timestamp(self):
assert t.value == expected
assert t.nanosecond == 5

t = Timestamp(np_datetime64_compat("2011-01-01 00:00:00.000000005Z"))
t = Timestamp("2011-01-01 00:00:00.000000005")
assert repr(t) == "Timestamp('2011-01-01 00:00:00.000000005')"
assert t.value == expected
assert t.nanosecond == 5
Expand All @@ -508,7 +507,7 @@ def test_nanosecond_timestamp(self):
assert t.value == expected
assert t.nanosecond == 10

t = Timestamp(np_datetime64_compat("2011-01-01 00:00:00.000000010Z"))
t = Timestamp("2011-01-01 00:00:00.000000010")
assert repr(t) == "Timestamp('2011-01-01 00:00:00.000000010')"
assert t.value == expected
assert t.nanosecond == 10
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/series/methods/test_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import pytest

from pandas._libs.tslibs import IncompatibleFrequency
from pandas.compat import np_datetime64_compat

from pandas import (
DatetimeIndex,
Expand All @@ -28,7 +27,7 @@ def test_asof_nanosecond_index_access(self):
# handle nanoseconds
assert first_value == ser["2013-01-01 00:00:00.000000050+0000"]

expected_ts = np_datetime64_compat("2013-01-01 00:00:00.000000050+0000", "ns")
expected_ts = np.datetime64("2013-01-01 00:00:00.000000050", "ns")
assert first_value == ser[Timestamp(expected_ts)]

def test_basic(self):
Expand Down
7 changes: 3 additions & 4 deletions pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
algos as libalgos,
hashtable as ht,
)
from pandas.compat import np_array_datetime64_compat
import pandas.util._test_decorators as td

from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -537,10 +536,10 @@ def test_dtype_preservation(self, any_numpy_dtype):

def test_datetime64_dtype_array_returned(self):
# GH 9431
expected = np_array_datetime64_compat(
expected = np.array(
[
"2015-01-03T00:00:00.000000000+0000",
"2015-01-01T00:00:00.000000000+0000",
"2015-01-03T00:00:00.000000000",
"2015-01-01T00:00:00.000000000",
],
dtype="M8[ns]",
)
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/tseries/offsets/test_business_day.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
timedelta,
)

import numpy as np
import pytest

from pandas._libs.tslibs.offsets import (
ApplyTypeError,
BDay,
BMonthEnd,
)
from pandas.compat import np_datetime64_compat

from pandas import (
DatetimeIndex,
Expand All @@ -36,7 +36,7 @@ class TestBusinessDay(Base):

def setup_method(self, method):
self.d = datetime(2008, 1, 1)
self.nd = np_datetime64_compat("2008-01-01 00:00:00Z")
self.nd = np.datetime64("2008-01-01 00:00:00")

self.offset = self._offset()
self.offset1 = self.offset
Expand Down
Loading