Skip to content

Commit

Permalink
fix zero length RangeIndex + add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
topper-123 committed Aug 21, 2020
1 parent ce8b8a4 commit c08d426
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ def _format_data(self, name=None):
return None

def _format_with_header(self, header: List[str], na_rep: str = "NaN") -> List[str]:
if len(self._range) == 0:
return []
first_val_str = str(self._range[0])
last_val_str = str(self._range[-1])
max_length = max(len(first_val_str), len(last_val_str))
Expand Down
8 changes: 6 additions & 2 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import gc
from typing import Optional, Type
from typing import Type

import numpy as np
import pytest
Expand Down Expand Up @@ -33,7 +33,7 @@
class Base:
""" base class for index sub-class tests """

_holder: Optional[Type[Index]] = None
_holder: Type[Index]
_compat_props = ["shape", "ndim", "size", "nbytes"]

def create_index(self) -> Index:
Expand Down Expand Up @@ -681,6 +681,10 @@ def test_format(self):
expected = [str(x) for x in idx]
assert idx.format() == expected

def test_format_empty(self):
# GH35712
assert self._holder([]).format() == []

def test_hasnans_isnans(self, index):
# GH 11343, added tests for hasnans / isnans
if isinstance(index, MultiIndex):
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/indexes/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,10 @@ def test_contains_raise_error_if_period_index_is_in_multi_index(self, msg, key):
with pytest.raises(KeyError, match=msg):
df.loc[key]

def test_format_empty(self):
# GH35712
assert self._holder([], freq="A").format() == []


def test_maybe_convert_timedelta():
pi = PeriodIndex(["2000", "2001"], freq="D")
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/indexes/ranges/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,7 @@ def test_engineless_lookup(self):
idx.get_loc("a")

assert "_engine" not in idx._cache

def test_format_empty(self):
# GH35712
assert self._holder(0).format() == []

0 comments on commit c08d426

Please sign in to comment.