Skip to content

Commit 02c4bc6

Browse files
committed
Address some of the code review responses
1 parent 1eb8918 commit 02c4bc6

File tree

4 files changed

+46
-49
lines changed

4 files changed

+46
-49
lines changed

pandas/core/indexes/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,7 @@ def is_monotonic(self):
12011201
return self.is_monotonic_increasing
12021202

12031203
@property
1204-
def is_strictly_monotonic(self):
1204+
def _is_strictly_monotonic(self):
12051205
""" Checks if the index is sorted """
12061206
return (self._is_strictly_monotonic_increasing or
12071207
self._is_strictly_monotonic_decreasing)

pandas/core/indexes/datetimelike.py

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from pandas._libs.period import Period
2929

3030
from pandas.core.indexes.base import Index, _index_shared_docs
31+
from pandas.tseries.offsets import index_offsets_equal
3132
from pandas.util._decorators import Appender, cache_readonly
3233
import pandas.core.dtypes.concat as _concat
3334
import pandas.tseries.frequencies as frequencies
@@ -854,11 +855,7 @@ def _concat_same_dtype(self, to_concat, name):
854855
new_data = np.concatenate([c.asi8 for c in to_concat])
855856
return self._simple_new(new_data, **attribs)
856857

857-
def _slice_by_value(self, start, end):
858-
sliced = self.values[slice(*self.slice_locs(start, end))]
859-
return self._shallow_copy(sliced)
860-
861-
def _intersect_ascending(self, other, **empty_params):
858+
def _intersect_ascending(self, other):
862859
# to make our life easier, "sort" the two ranges
863860
if self[0] <= other[0]:
864861
left, right = self, other
@@ -869,10 +866,10 @@ def _intersect_ascending(self, other, **empty_params):
869866
start = right[0]
870867

871868
if end < start:
872-
return type(self)(data=[], **empty_params)
873-
return left._slice_by_value(start, end)
869+
return []
870+
return left.values[slice(*left.slice_locs(start, end))]
874871

875-
def _intersect_descending(self, other, **empty_params):
872+
def _intersect_descending(self, other):
876873
# this is essentially a flip of _intersect_ascending
877874
if self[0] >= other[0]:
878875
left, right = self, other
@@ -883,17 +880,8 @@ def _intersect_descending(self, other, **empty_params):
883880
end = right[-1]
884881

885882
if end > start:
886-
return type(self)(data=[], **empty_params)
887-
return left._slice_by_value(start, end)
888-
889-
def _offsets_equal(self, other):
890-
offsets_equal = True
891-
self_offset = getattr(self, 'offset', None)
892-
other_offset = getattr(other, 'offset', None)
893-
if self_offset is None or other_offset is None or \
894-
self_offset != other_offset or other_offset.isAnchored():
895-
offsets_equal = False
896-
return offsets_equal
883+
return Index()
884+
return left.values[slice(*left.slice_locs(start, end))]
897885

898886
def intersection(self, other):
899887
"""
@@ -914,55 +902,44 @@ def intersection(self, other):
914902
if self.equals(other):
915903
return self._get_consensus_name(other)
916904

917-
if not isinstance(other, DatetimeIndexOpsMixin):
918-
try:
919-
other = DatetimeIndexOpsMixin(other)
920-
except (TypeError, ValueError):
921-
pass
905+
lengths = len(self), len(other)
906+
if lengths[0] == 0:
907+
return self
908+
if lengths[1] == 0:
909+
return other
910+
911+
if not isinstance(other, Index):
922912
result = Index.intersection(self, other)
923913
return result
924-
elif (self._offsets_equal(other) or
925-
(not self.is_strictly_monotonic or
926-
not other.is_strictly_monotonic)):
914+
elif (index_offsets_equal(self, other) or
915+
(not self._is_strictly_monotonic or
916+
not other._is_strictly_monotonic)):
927917
result = Index.intersection(self, other)
928-
tz = getattr(self, 'tz', None)
929918
result = self._shallow_copy(result._values, name=result.name,
930-
tz=tz, freq=None)
919+
tz=getattr(self, 'tz', None))
931920
if result.freq is None:
932921
result.offset = frequencies.to_offset(result.inferred_freq)
933922
return result
934923

935-
lenghts = len(self), len(other)
936-
if lenghts[0] == 0:
937-
return self
938-
if lenghts[1] == 0:
939-
return other
940-
941924
# handle intersecting things like this
942925
# idx1 = pd.to_timedelta((1, 2, 3, 4, 5, 6, 7, 8), unit='s')
943926
# idx2 = pd.to_timedelta((2, 3, 4, 8), unit='s')
944-
if lenghts[0] != lenghts[1] and (
945-
max(self) != max(other) or min(self) != min(other)):
927+
if lengths[0] != lengths[1] and (
928+
max(self) != max(other) or min(self) != min(other)):
946929
return Index.intersection(self, other)
947930

948931
# coerce into same order
949932
self_ascending = self.is_monotonic_increasing
950933
if self_ascending != other.is_monotonic_increasing:
951934
other = other.sort_values(ascending=self_ascending)
952935

953-
# Thanks, PeriodIndex
954-
empty_params = {'freq': getattr(self, 'freq', None)}
955-
956936
if self_ascending:
957-
intersected = self._intersect_ascending(other, **empty_params)
937+
intersected_slice = self._intersect_ascending(other)
958938
else:
959-
intersected = self._intersect_descending(other, **empty_params)
939+
intersected_slice = self._intersect_descending(other)
960940

961-
name = self.name
962-
if self.name != other.name:
963-
name = None
964-
intersected.name = name
965-
return intersected
941+
intersected = self._shallow_copy(intersected_slice)
942+
return intersected._get_consensus_name(other)
966943

967944

968945
def _ensure_datetimelike_to_i8(other):

pandas/core/indexes/period.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ def _maybe_cast_slice_bound(self, label, side, kind):
843843
Value of `side` parameter should be validated in caller.
844844
845845
"""
846-
assert kind in ['ix', 'loc', 'getitem', None]
846+
assert kind in ['ix', 'loc', 'getitem']
847847

848848
if isinstance(label, datetime):
849849
return Period(label, freq=self.freq)

pandas/tseries/offsets.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,26 @@ def _is_normalized(dt):
119119
return False
120120
return True
121121

122+
123+
def index_offsets_equal(first, second):
124+
"""
125+
Checks if the two indexes have an offset, and if they equal each other
126+
Parameters
127+
----------
128+
first: Index
129+
second: Index
130+
131+
Returns
132+
-------
133+
bool
134+
"""
135+
first = getattr(first, 'offset', None)
136+
second = getattr(second, 'offset', None)
137+
are_offsets_equal = True
138+
if first is None or second is None or first != second:
139+
are_offsets_equal = False
140+
return are_offsets_equal
141+
122142
# ---------------------------------------------------------------------
123143
# DateOffset
124144

0 commit comments

Comments
 (0)