Skip to content

Commit 7495698

Browse files
fix: allow comparison with scalar values (#88)
* fix: address failing compliance tests in DateArray and TimeArray test: add a test session with prerelease versions of dependencies * fix min/max/median for 2D arrays * fixes except for null contains * actually use NaT as 'advertised' * fix!: use `pandas.NaT` for missing values in dbdate and dbtime dtypes This makes them consistent with other date/time dtypes, as well as internally consistent with the advertised `dtype.na_value`. BREAKING-CHANGE: dbdate and dbtime dtypes return NaT instead of None for missing values Release-As: 0.4.0 * more progress towards compliance * address errors in TestMethods * move tests * add prerelease deps * fix: address failing tests with pandas 1.5.0 test: add a test session with prerelease versions of dependencies * fix owlbot config * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * document why microsecond precision is used * use correct units * add box_func tests * typo * fix: avoid TypeError when using sorted search * add unit tests * fix: dbdate and dbtime support set item * add TestMethods * fix: allow comparison with scalar values * correct behavior for comparison with different types and shapes * use same dtype in shape comparison tests Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 42bc2d9 commit 7495698

File tree

4 files changed

+92
-10
lines changed

4 files changed

+92
-10
lines changed

db_dtypes/core.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ def _cmp_method(self, other, op):
9090
if is_scalar(other) and (pandas.isna(other) or type(other) == self.dtype.type):
9191
other = type(self)([other])
9292

93+
if type(other) != type(self):
94+
return NotImplemented
95+
9396
oshape = getattr(other, "shape", None)
9497
if oshape != self.shape and oshape != (1,) and self.shape != (1,):
9598
raise TypeError(
9699
"Can't compare arrays with different shapes", self.shape, oshape
97100
)
98-
99-
if type(other) != type(self):
100-
return NotImplemented
101101
return op(self._ndarray, other._ndarray)
102102

103103
def _from_factorized(self, unique, original):

tests/compliance/conftest.py

+62
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,23 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import operator
16+
1517
import pandas
1618
import pytest
1719

1820

21+
@pytest.fixture(params=[True, False])
22+
def as_array(request):
23+
"""
24+
Boolean fixture to support ExtensionDtype _from_sequence method testing.
25+
26+
See:
27+
https://github.com/pandas-dev/pandas/blob/main/pandas/tests/extension/conftest.py
28+
"""
29+
return request.param
30+
31+
1932
@pytest.fixture(params=[True, False])
2033
def as_frame(request):
2134
"""
@@ -38,6 +51,36 @@ def as_series(request):
3851
return request.param
3952

4053

54+
@pytest.fixture(params=[True, False])
55+
def box_in_series(request):
56+
"""
57+
Whether to box the data in a Series
58+
59+
See:
60+
https://github.com/pandas-dev/pandas/blob/main/pandas/tests/extension/conftest.py
61+
"""
62+
return request.param
63+
64+
65+
@pytest.fixture(
66+
params=[
67+
operator.eq,
68+
operator.ne,
69+
operator.gt,
70+
operator.ge,
71+
operator.lt,
72+
operator.le,
73+
]
74+
)
75+
def comparison_op(request):
76+
"""
77+
Fixture for operator module comparison functions.
78+
79+
See: https://github.com/pandas-dev/pandas/blob/main/pandas/conftest.py
80+
"""
81+
return request.param
82+
83+
4184
@pytest.fixture(params=["ffill", "bfill"])
4285
def fillna_method(request):
4386
"""
@@ -50,6 +93,25 @@ def fillna_method(request):
5093
return request.param
5194

5295

96+
@pytest.fixture(
97+
params=[
98+
lambda x: 1,
99+
lambda x: [1] * len(x),
100+
lambda x: pandas.Series([1] * len(x)),
101+
lambda x: x,
102+
],
103+
ids=["scalar", "list", "series", "object"],
104+
)
105+
def groupby_apply_op(request):
106+
"""
107+
Functions to test groupby.apply().
108+
109+
See:
110+
https://github.com/pandas-dev/pandas/blob/main/pandas/tests/extension/conftest.py
111+
"""
112+
return request.param
113+
114+
53115
@pytest.fixture
54116
def invalid_scalar(data):
55117
"""

tests/compliance/date/test_date_compliance.py

+24
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,27 @@ def test_value_counts(self, all_data, dropna):
7474
expected = pandas.Series(other).value_counts(dropna=dropna).sort_index()
7575

7676
self.assert_series_equal(result, expected)
77+
78+
79+
class TestCasting(base.BaseCastingTests):
80+
pass
81+
82+
83+
class TestGroupby(base.BaseGroupbyTests):
84+
pass
85+
86+
87+
class TestSetitem(base.BaseSetitemTests):
88+
pass
89+
90+
91+
class TestPrinting(base.BasePrintingTests):
92+
pass
93+
94+
95+
# TODO(https://github.com/googleapis/python-db-dtypes-pandas/issues/78): Add
96+
# compliance tests for arithmetic operations.
97+
98+
99+
class TestComparisonOps(base.BaseComparisonOpsTests):
100+
pass

tests/unit/test_dtypes.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,12 @@ def test_timearray_comparisons(
169169
np.testing.assert_array_equal(comparisons[op](left, r), expected)
170170
np.testing.assert_array_equal(complements[op](left, r), ~expected)
171171

172-
# Bad shape
173-
for bad_shape in ([], [1, 2, 3]):
172+
# Bad shape, but same type
173+
for bad_shape in ([], sample_values[:3]):
174174
with pytest.raises(
175175
TypeError, match="Can't compare arrays with different shapes"
176176
):
177-
comparisons[op](left, np.array(bad_shape))
178-
with pytest.raises(
179-
TypeError, match="Can't compare arrays with different shapes"
180-
):
181-
complements[op](left, np.array(bad_shape))
177+
comparisons[op](left, _cls(dtype)._from_sequence(bad_shape))
182178

183179
# Bad items
184180
for bad_items in (

0 commit comments

Comments
 (0)