Skip to content

TYPES: __len__, is_all_dates, inferred_type #29475

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

Merged
merged 5 commits into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions pandas/_libs/hashtable_class_helper.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ cdef class {{name}}Vector:
PyMem_Free(self.data)
self.data = NULL

def __len__(self):
def __len__(self) -> int:
return self.data.n

cpdef to_array(self):
Expand Down Expand Up @@ -168,7 +168,7 @@ cdef class StringVector:
PyMem_Free(self.data)
self.data = NULL

def __len__(self):
def __len__(self) -> int:
return self.data.n

def to_array(self):
Expand Down Expand Up @@ -212,7 +212,7 @@ cdef class ObjectVector:
self.ao = np.empty(_INIT_VEC_CAP, dtype=object)
self.data = <PyObject**>self.ao.data

def __len__(self):
def __len__(self) -> int:
return self.n

cdef inline append(self, object obj):
Expand Down Expand Up @@ -270,7 +270,7 @@ cdef class {{name}}HashTable(HashTable):
size_hint = min(size_hint, _SIZE_HINT_LIMIT)
kh_resize_{{dtype}}(self.table, size_hint)

def __len__(self):
def __len__(self) -> int:
return self.table.size

def __dealloc__(self):
Expand Down Expand Up @@ -897,7 +897,7 @@ cdef class PyObjectHashTable(HashTable):
kh_destroy_pymap(self.table)
self.table = NULL

def __len__(self):
def __len__(self) -> int:
return self.table.size

def __contains__(self, object key):
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/internals.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ cdef class BlockPlacement:
def __repr__(self) -> str:
return str(self)

def __len__(self):
def __len__(self) -> int:
cdef:
slice s = self._ensure_has_slice()
if s is not None:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1940,7 +1940,7 @@ def take_nd(self, indexer, allow_fill=None, fill_value=None):

take = take_nd

def __len__(self):
def __len__(self) -> int:
"""
The length of this Categorical.
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ def size(self) -> int:
"""The number of elements in this array."""
return np.prod(self.shape)

def __len__(self):
def __len__(self) -> int:
return len(self._data)

def __getitem__(self, key):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ def __setitem__(self, key, value):
self._data[key] = value
self._mask[key] = mask

def __len__(self):
def __len__(self) -> int:
return len(self._data)

@property
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ def _validate(self):
def __iter__(self):
return iter(np.asarray(self))

def __len__(self):
def __len__(self) -> int:
return len(self.left)

def __getitem__(self, value):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/computation/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ def __call__(self):
def __repr__(self) -> str:
return printing.pprint_thing(self.terms)

def __len__(self):
def __len__(self) -> int:
return len(self.expr)

def parse(self):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ def itertuples(self, index=True, name="Pandas"):
# fallback to regular tuples
return zip(*arrays)

def __len__(self):
def __len__(self) -> int:
"""
Returns length of info axis, but here we use the index.
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1951,7 +1951,7 @@ def items(self):
def iteritems(self):
return self.items()

def __len__(self):
def __len__(self) -> int:
"""Returns length of info axis"""
return len(self._info_axis)

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def __init__(
# we accept no other args
validate_kwargs("group", kwargs, {})

def __len__(self):
def __len__(self) -> int:
return len(self.groups)

def __repr__(self) -> str:
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,10 +649,12 @@ def _engine(self):
# Array-Like Methods

# ndarray compat
def __len__(self):
def __len__(self) -> int:
"""
Return the length of the Index.
"""
# Assertion needed for mypy, see GH#29475
assert self._data is not None
return len(self._data)

def __array__(self, dtype=None):
Expand Down Expand Up @@ -1807,7 +1809,7 @@ def inferred_type(self):
return lib.infer_dtype(self, skipna=False)

@cache_readonly
def is_all_dates(self):
def is_all_dates(self) -> bool:
return is_datetime_array(ensure_object(self.values))

# --------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ def _format_attrs(self):
# --------------------------------------------------------------------

@property
def inferred_type(self):
def inferred_type(self) -> str:
return "categorical"

@property
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1235,13 +1235,13 @@ def is_type_compatible(self, typ):
return typ == self.inferred_type or typ == "datetime"

@property
def inferred_type(self):
def inferred_type(self) -> str:
# b/c datetime is represented as microseconds since the epoch, make
# sure we can't have ambiguous indexing
return "datetime64"

@property
def is_all_dates(self):
def is_all_dates(self) -> bool:
return True

def insert(self, loc, item):
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def itemsize(self):
warnings.simplefilter("ignore")
return self.left.itemsize + self.right.itemsize

def __len__(self):
def __len__(self) -> int:
return len(self.left)

@cache_readonly
Expand Down Expand Up @@ -524,7 +524,7 @@ def dtype(self):
return self._data.dtype

@property
def inferred_type(self):
def inferred_type(self) -> str:
"""Return a string of the type inferred from the values"""
return "interval"

Expand Down Expand Up @@ -1357,7 +1357,7 @@ def func(self, other, sort=sort):
return func

@property
def is_all_dates(self):
def is_all_dates(self) -> bool:
"""
This is False even when left/right contain datetime-like objects,
as the check is done on the Interval itself
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,7 @@ def format(

# --------------------------------------------------------------------

def __len__(self):
def __len__(self) -> int:
return len(self.codes[0])

def _get_names(self):
Expand Down Expand Up @@ -1322,7 +1322,7 @@ def _constructor(self):
return MultiIndex.from_tuples

@cache_readonly
def inferred_type(self):
def inferred_type(self) -> str:
return "mixed"

def _get_level_number(self, level):
Expand Down Expand Up @@ -1791,7 +1791,7 @@ def to_flat_index(self):
return Index(self.values, tupleize_cols=False)

@property
def is_all_dates(self):
def is_all_dates(self) -> bool:
return False

def is_lexsorted(self):
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def _concat_same_dtype(self, indexes, name):
return result.rename(name)

@property
def is_all_dates(self):
def is_all_dates(self) -> bool:
"""
Checks that all the labels are datetime objects
"""
Expand Down Expand Up @@ -227,7 +227,7 @@ class Int64Index(IntegerIndex):
_default_dtype = np.int64

@property
def inferred_type(self):
def inferred_type(self) -> str:
"""Always 'integer' for ``Int64Index``"""
return "integer"

Expand Down Expand Up @@ -282,7 +282,7 @@ class UInt64Index(IntegerIndex):
_default_dtype = np.uint64

@property
def inferred_type(self):
def inferred_type(self) -> str:
"""Always 'integer' for ``UInt64Index``"""
return "integer"

Expand Down Expand Up @@ -355,7 +355,7 @@ class Float64Index(NumericIndex):
_default_dtype = np.float64

@property
def inferred_type(self):
def inferred_type(self) -> str:
"""Always 'floating' for ``Float64Index``"""
return "floating"

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ def searchsorted(self, value, side="left", sorter=None):
return self._ndarray_values.searchsorted(value, side=side, sorter=sorter)

@property
def is_all_dates(self):
def is_all_dates(self) -> bool:
return True

@property
Expand All @@ -591,7 +591,7 @@ def is_full(self):
return ((values[1:] - values[:-1]) < 2).all()

@property
def inferred_type(self):
def inferred_type(self) -> str:
# b/c data is represented as ints make sure we can't have ambiguous
# indexing
return "period"
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ def _concat_same_dtype(self, indexes, name):
# In this case return an empty range index.
return RangeIndex(0, 0).rename(name)

def __len__(self):
def __len__(self) -> int:
"""
return the length of the RangeIndex
"""
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,11 +602,11 @@ def is_type_compatible(self, typ):
return typ == self.inferred_type or typ == "timedelta"

@property
def inferred_type(self):
def inferred_type(self) -> str:
return "timedelta64"

@property
def is_all_dates(self):
def is_all_dates(self) -> bool:
return True

def insert(self, loc, item):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def __repr__(self) -> str:

return result

def __len__(self):
def __len__(self) -> int:
return len(self.values)

def __getstate__(self):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def _post_setstate(self):
self._known_consolidated = False
self._rebuild_blknos_and_blklocs()

def __len__(self):
def __len__(self) -> int:
return len(self.items)

def __repr__(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ def put(self, *args, **kwargs):
)
self._values.put(*args, **kwargs)

def __len__(self):
def __len__(self) -> int:
"""
Return the length of the Series.
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ def __contains__(self, key):
return True
return False

def __len__(self):
def __len__(self) -> int:
return len(self.groups())

def __repr__(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/reshape/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1879,7 +1879,7 @@ def test_concat_iterables(self):
tm.assert_frame_equal(concat(deque((df1, df2)), ignore_index=True), expected)

class CustomIterator1:
def __len__(self):
def __len__(self) -> int:
return 2

def __getitem__(self, index):
Expand Down
2 changes: 1 addition & 1 deletion scripts/validate_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def __init__(self, name):
self.clean_doc = pydoc.getdoc(obj)
self.doc = NumpyDocString(self.clean_doc)

def __len__(self):
def __len__(self) -> int:
return len(self.raw_doc)

@staticmethod
Expand Down