|
| 1 | +from datetime import datetime |
| 2 | +import sys |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import pytest |
| 6 | + |
| 7 | +from pandas.compat import PYPY |
| 8 | + |
| 9 | +import pandas as pd |
| 10 | +from pandas import DataFrame, Index, Series |
| 11 | +from pandas.core.accessor import PandasDelegate |
| 12 | +from pandas.core.base import NoNewAttributesMixin, PandasObject |
| 13 | +import pandas.util.testing as tm |
| 14 | + |
| 15 | + |
| 16 | +class TestPandasDelegate: |
| 17 | + class Delegator: |
| 18 | + _properties = ["foo"] |
| 19 | + _methods = ["bar"] |
| 20 | + |
| 21 | + def _set_foo(self, value): |
| 22 | + self.foo = value |
| 23 | + |
| 24 | + def _get_foo(self): |
| 25 | + return self.foo |
| 26 | + |
| 27 | + foo = property(_get_foo, _set_foo, doc="foo property") |
| 28 | + |
| 29 | + def bar(self, *args, **kwargs): |
| 30 | + """ a test bar method """ |
| 31 | + pass |
| 32 | + |
| 33 | + class Delegate(PandasDelegate, PandasObject): |
| 34 | + def __init__(self, obj): |
| 35 | + self.obj = obj |
| 36 | + |
| 37 | + def setup_method(self, method): |
| 38 | + pass |
| 39 | + |
| 40 | + def test_invalid_delegation(self): |
| 41 | + # these show that in order for the delegation to work |
| 42 | + # the _delegate_* methods need to be overridden to not raise |
| 43 | + # a TypeError |
| 44 | + |
| 45 | + self.Delegate._add_delegate_accessors( |
| 46 | + delegate=self.Delegator, |
| 47 | + accessors=self.Delegator._properties, |
| 48 | + typ="property", |
| 49 | + ) |
| 50 | + self.Delegate._add_delegate_accessors( |
| 51 | + delegate=self.Delegator, accessors=self.Delegator._methods, typ="method" |
| 52 | + ) |
| 53 | + |
| 54 | + delegate = self.Delegate(self.Delegator()) |
| 55 | + |
| 56 | + with pytest.raises(TypeError): |
| 57 | + delegate.foo |
| 58 | + |
| 59 | + with pytest.raises(TypeError): |
| 60 | + delegate.foo = 5 |
| 61 | + |
| 62 | + with pytest.raises(TypeError): |
| 63 | + delegate.foo() |
| 64 | + |
| 65 | + @pytest.mark.skipif(PYPY, reason="not relevant for PyPy") |
| 66 | + def test_memory_usage(self): |
| 67 | + # Delegate does not implement memory_usage. |
| 68 | + # Check that we fall back to in-built `__sizeof__` |
| 69 | + # GH 12924 |
| 70 | + delegate = self.Delegate(self.Delegator()) |
| 71 | + sys.getsizeof(delegate) |
| 72 | + |
| 73 | + |
| 74 | +class TestNoNewAttributesMixin: |
| 75 | + def test_mixin(self): |
| 76 | + class T(NoNewAttributesMixin): |
| 77 | + pass |
| 78 | + |
| 79 | + t = T() |
| 80 | + assert not hasattr(t, "__frozen") |
| 81 | + |
| 82 | + t.a = "test" |
| 83 | + assert t.a == "test" |
| 84 | + |
| 85 | + t._freeze() |
| 86 | + assert "__frozen" in dir(t) |
| 87 | + assert getattr(t, "__frozen") |
| 88 | + |
| 89 | + with pytest.raises(AttributeError): |
| 90 | + t.b = "test" |
| 91 | + |
| 92 | + assert not hasattr(t, "b") |
| 93 | + |
| 94 | + |
| 95 | +class TestConstruction: |
| 96 | + # test certain constructor behaviours on dtype inference across Series, |
| 97 | + # Index and DataFrame |
| 98 | + |
| 99 | + @pytest.mark.parametrize( |
| 100 | + "klass", |
| 101 | + [ |
| 102 | + Series, |
| 103 | + lambda x, **kwargs: DataFrame({"a": x}, **kwargs)["a"], |
| 104 | + pytest.param( |
| 105 | + lambda x, **kwargs: DataFrame(x, **kwargs)[0], marks=pytest.mark.xfail |
| 106 | + ), |
| 107 | + Index, |
| 108 | + ], |
| 109 | + ) |
| 110 | + @pytest.mark.parametrize( |
| 111 | + "a", |
| 112 | + [ |
| 113 | + np.array(["2263-01-01"], dtype="datetime64[D]"), |
| 114 | + np.array([datetime(2263, 1, 1)], dtype=object), |
| 115 | + np.array([np.datetime64("2263-01-01", "D")], dtype=object), |
| 116 | + np.array(["2263-01-01"], dtype=object), |
| 117 | + ], |
| 118 | + ids=[ |
| 119 | + "datetime64[D]", |
| 120 | + "object-datetime.datetime", |
| 121 | + "object-numpy-scalar", |
| 122 | + "object-string", |
| 123 | + ], |
| 124 | + ) |
| 125 | + def test_constructor_datetime_outofbound(self, a, klass): |
| 126 | + # GH-26853 (+ bug GH-26206 out of bound non-ns unit) |
| 127 | + |
| 128 | + # No dtype specified (dtype inference) |
| 129 | + # datetime64[non-ns] raise error, other cases result in object dtype |
| 130 | + # and preserve original data |
| 131 | + if a.dtype.kind == "M": |
| 132 | + with pytest.raises(pd.errors.OutOfBoundsDatetime): |
| 133 | + klass(a) |
| 134 | + else: |
| 135 | + result = klass(a) |
| 136 | + assert result.dtype == "object" |
| 137 | + tm.assert_numpy_array_equal(result.to_numpy(), a) |
| 138 | + |
| 139 | + # Explicit dtype specified |
| 140 | + # Forced conversion fails for all -> all cases raise error |
| 141 | + with pytest.raises(pd.errors.OutOfBoundsDatetime): |
| 142 | + klass(a, dtype="datetime64[ns]") |
0 commit comments