Skip to content
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

Fix .loc to work properly when using 'slice' #1159

Merged
merged 14 commits into from
Jan 8, 2020
28 changes: 17 additions & 11 deletions databricks/koalas/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,12 @@ class LocIndexer(_LocIndexerLike):
Slice with labels for row and single label for column. As mentioned
above, note that both the start and stop of the slice are included.

Also note that the row for 'sidewinder' is included since 'sidewinder'
is between 'cobra' and 'viper'.
Also note that the row for 'sidewinder' is not included since 'sidewinder'
is not between 'cobra' and 'viper'.
itholic marked this conversation as resolved.
Show resolved Hide resolved

>>> df.loc['cobra':'viper', 'max_speed']
cobra 1
viper 4
sidewinder 7
cobra 1
viper 4
Name: max_speed, dtype: int64

Conditional that returns a boolean Series
Expand Down Expand Up @@ -400,16 +399,23 @@ def _select_rows(self, rows_sel):
# If slice is None - select everything, so nothing to do
return None, None
elif len(self._internal.index_columns) == 1:
start = rows_sel.start
stop = rows_sel.stop

sdf = self._kdf_or_kser._internal.sdf
index_column = self._kdf_or_kser.index.to_series()
index_data_type = index_column.spark_type

# get natural order from '__natural_order__' from start to stop
# based on index_columns to keep natural order.
start = sdf.select(NATURAL_ORDER_COLUMN_NAME) \
.where(index_column._scol == rows_sel.start) \
.first()[0]
itholic marked this conversation as resolved.
Show resolved Hide resolved
stop = sdf.select(NATURAL_ORDER_COLUMN_NAME) \
.where(index_column._scol == rows_sel.stop) \
.first()[0]
HyukjinKwon marked this conversation as resolved.
Show resolved Hide resolved

cond = []
if start is not None:
cond.append(index_column._scol >= F.lit(start).cast(index_data_type))
cond.append(sdf[NATURAL_ORDER_COLUMN_NAME] >= F.lit(start))
if stop is not None:
cond.append(index_column._scol <= F.lit(stop).cast(index_data_type))
cond.append(sdf[NATURAL_ORDER_COLUMN_NAME] <= F.lit(stop))

if len(cond) > 0:
return reduce(lambda x, y: x & y, cond), None
Expand Down