Skip to content

Commit 587cc23

Browse files
authored
Merge pull request #12 from jseabold/gh-8
Support dt property comparisons and between
2 parents 93fb369 + b5e1a54 commit 587cc23

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

pandas_selectable/__init__.py

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import operator
33
from functools import wraps
44

5+
import numpy as np
56
import pandas as pd
67
from pandas.core.accessor import CachedAccessor
78
from pandas.core.indexes.accessors import (
@@ -70,31 +71,6 @@ def _wrap_result(self, *args, **kwargs):
7071
return self._parent_frame.loc[bool_idx]
7172

7273

73-
class SelectDatetimeProperties(DatetimeProperties):
74-
def __init__(self, parent, *args, **kwargs):
75-
# datetime properties holds an attribute _parent
76-
# we need to add the parent_frame (or series) to the subclass instances
77-
self._parent_frame = parent
78-
super().__init__(*args, **kwargs)
79-
80-
def __getattribute__(self, attr):
81-
if (
82-
not attr.startswith("_")
83-
and inspect.isroutine( # noqa
84-
getattr(DatetimeProperties, attr, None)
85-
)
86-
and attr not in _date_boolean_methods
87-
): # noqa
88-
raise NotImplementedError(
89-
"Boolean selection with this method " "does not make sense."
90-
)
91-
elif attr in _date_boolean_methods:
92-
idx = super().__getattribute__(attr)
93-
return self._parent_frame.loc[idx]
94-
else:
95-
return super().__getattribute__(attr)
96-
97-
9874
class SelectPeriodProperties(PeriodProperties):
9975
def __init__(self, parent, *args, **kwargs):
10076
self._parent_frame = parent
@@ -207,6 +183,7 @@ class SelectableColumn:
207183
notna = selector_wrapper(pd.Series, "notna")
208184
notnull = selector_wrapper(pd.Series, "notnull")
209185
isin = selector_wrapper(pd.Series, "isin")
186+
between = selector_wrapper(pd.Series, "between")
210187

211188
def __init__(self, parent, series=None):
212189
# if accessed as the series accessor, parent is the series
@@ -252,3 +229,37 @@ def __getitem__(self, key):
252229
@property
253230
def index(self):
254231
return SelectableIndex(self._frame)
232+
233+
234+
class SelectDatetimeProperties(DatetimeProperties):
235+
def __init__(self, parent, *args, **kwargs):
236+
# datetime properties holds an attribute _parent
237+
# we need to add the parent_frame (or series) to the subclass instances
238+
self._parent_frame = parent
239+
super().__init__(*args, **kwargs)
240+
241+
def __getattribute__(self, attr):
242+
if (
243+
not attr.startswith("_")
244+
and inspect.isroutine( # noqa
245+
getattr(DatetimeProperties, attr, None)
246+
)
247+
and attr not in _date_boolean_methods
248+
): # noqa
249+
raise NotImplementedError(
250+
"Boolean selection with this method " "does not make sense."
251+
)
252+
elif attr in _date_boolean_methods:
253+
idx = super().__getattribute__(attr)
254+
return self._parent_frame.loc[idx]
255+
else:
256+
got_attr = super().__getattribute__(attr)
257+
# this allows things like dt.day, dt.month to be selectable
258+
# for the parent frame. assumes they're all properties.
259+
if (
260+
isinstance(got_attr, pd.Series)
261+
and not attr.startswith('_')
262+
and isinstance(getattr(self.__class__, attr), property)
263+
):
264+
return SelectableColumn(self._parent_frame, got_attr)
265+
return got_attr

pandas_selectable/tests/test_select.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ def test_dt(dta):
5656
assert dta.select.B.dt.is_quarter_start.equals(dta.loc[[0]])
5757
assert len(dta.select.B.dt.is_year_end) == 0
5858
assert dta.select.B.dt.is_year_start.equals(dta.loc[[0]])
59+
assert len(dta.select.B.dt.day == 15) == 1
60+
assert len(dta.select.B.dt.month == 2) == 0
61+
assert len(dta.select.B.between("2020-01-03", "2020-01-05")) == 3
5962

6063

6164
def test_index(dta):

0 commit comments

Comments
 (0)