Skip to content

Some code cleanups #31462

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 1 commit into from
Jan 31, 2020
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
70 changes: 34 additions & 36 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Base and utility classes for pandas objects.
"""

import builtins
import textwrap
from typing import Dict, FrozenSet, List, Optional, Union
Expand Down Expand Up @@ -45,11 +46,15 @@


class PandasObject(DirNamesMixin):
"""baseclass for various pandas objects"""
"""
Baseclass for various pandas objects.
"""

@property
def _constructor(self):
"""class constructor (for this class it's just `__class__`"""
"""
Class constructor (for this class it's just `__class__`.
"""
return type(self)

def __repr__(self) -> str:
Expand Down Expand Up @@ -77,16 +82,14 @@ def __sizeof__(self):
"""
if hasattr(self, "memory_usage"):
mem = self.memory_usage(deep=True)
if not is_scalar(mem):
mem = mem.sum()
return int(mem)
return int(mem if is_scalar(mem) else mem.sum())

# no memory_usage attribute, so fall back to
# object's 'sizeof'
# no memory_usage attribute, so fall back to object's 'sizeof'
return super().__sizeof__()

def _ensure_type(self: T, obj) -> T:
"""Ensure that an object has same type as self.
"""
Ensure that an object has same type as self.

Used by type checkers.
"""
Expand All @@ -95,7 +98,8 @@ def _ensure_type(self: T, obj) -> T:


class NoNewAttributesMixin:
"""Mixin which prevents adding new attributes.
"""
Mixin which prevents adding new attributes.

Prevents additional attributes via xxx.attribute = "something" after a
call to `self.__freeze()`. Mainly used to prevent the user from using
Expand All @@ -106,7 +110,9 @@ class NoNewAttributesMixin:
"""

def _freeze(self):
"""Prevents setting additional attributes"""
"""
Prevents setting additional attributes.
"""
object.__setattr__(self, "__frozen", True)

# prevent adding any attribute via s.xxx.new_attribute = ...
Expand Down Expand Up @@ -180,14 +186,12 @@ class SelectionMixin:
@property
def _selection_name(self):
"""
return a name for myself; this would ideally be called
the 'name' property, but we cannot conflict with the
Series.name property which can be set
Return a name for myself;

This would ideally be called the 'name' property,
but we cannot conflict with the Series.name property which can be set.
"""
if self._selection is None:
return None # 'result'
else:
return self._selection
return self._selection

@property
def _selection_list(self):
Expand All @@ -199,7 +203,6 @@ def _selection_list(self):

@cache_readonly
def _selected_obj(self):

if self._selection is None or isinstance(self.obj, ABCSeries):
return self.obj
else:
Expand Down Expand Up @@ -246,12 +249,11 @@ def _gotitem(self, key, ndim: int, subset=None):

Parameters
----------
key : string / list of selections
key : str / list of selections
ndim : 1,2
requested ndim of result
subset : object, default None
subset to act on

"""
raise AbstractMethodError(self)

Expand All @@ -266,7 +268,6 @@ def _try_aggregate_string_function(self, arg: str, *args, **kwargs):
- try to find a function (or attribute) on ourselves
- try to find a numpy function
- raise

"""
assert isinstance(arg, str)

Expand Down Expand Up @@ -585,7 +586,6 @@ def _shallow_copy(self, obj, **kwargs):
"""
return a new object with the replacement attributes
"""

if isinstance(obj, self._constructor):
obj = obj.obj
for attr in self._attributes:
Expand Down Expand Up @@ -669,8 +669,7 @@ def item(self):

if len(self) == 1:
return next(iter(self))
else:
raise ValueError("can only convert an array of size 1 to a Python scalar")
raise ValueError("can only convert an array of size 1 to a Python scalar")

@property
def nbytes(self) -> int:
Expand Down Expand Up @@ -735,7 +734,6 @@ def array(self) -> ExtensionArray:

Examples
--------

For regular NumPy types like int, and float, a PandasArray
is returned.

Expand Down Expand Up @@ -851,12 +849,11 @@ def to_numpy(self, dtype=None, copy=False, na_value=lib.no_default, **kwargs):
"""
if is_extension_array_dtype(self.dtype):
return self.array.to_numpy(dtype, copy=copy, na_value=na_value, **kwargs)
else:
if kwargs:
msg = "to_numpy() got an unexpected keyword argument '{}'".format(
list(kwargs.keys())[0]
)
raise TypeError(msg)
elif kwargs:
bad_keys = list(kwargs.keys())[0]
raise TypeError(
f"to_numpy() got an unexpected keyword argument '{bad_keys}'"
)

result = np.asarray(self._values, dtype=dtype)
# TODO(GH-24345): Avoid potential double copy
Expand Down Expand Up @@ -1076,7 +1073,9 @@ def _reduce(
filter_type=None,
**kwds,
):
""" perform the reduction type operation if we can """
"""
Perform the reduction type operation if we can.
"""
func = getattr(self, name, None)
if func is None:
raise TypeError(
Expand All @@ -1103,9 +1102,7 @@ def _map_values(self, mapper, na_action=None):
The output of the mapping function applied to the index.
If the function returns a tuple with more than one element
a MultiIndex will be returned.

"""

# we can fastpath dict/Series to an efficient map
# as we know that we are not going to have to yield
# python types
Expand Down Expand Up @@ -1341,7 +1338,9 @@ def is_monotonic(self) -> bool:

@property
def is_monotonic_increasing(self) -> bool:
"""alias for is_monotonic"""
"""
Alias for is_monotonic.
"""
# mypy complains if we alias directly
return self.is_monotonic

Expand Down Expand Up @@ -1455,7 +1454,6 @@ def factorize(self, sort=False, na_sentinel=-1):

Examples
--------

>>> x = pd.Series([1, 2, 3])
>>> x
0 1
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,8 +874,8 @@ def style(self) -> "Styler":
polar bear 22000
koala marsupial 80000
>>> for label, content in df.items():
... print('label:', label)
... print('content:', content, sep='\n')
... print(f'label: {label}')
... print(f'content: {content}', sep='\n')
...
label: species
content:
Expand Down
11 changes: 6 additions & 5 deletions pandas/io/excel/_pyxlsb.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

class _PyxlsbReader(_BaseExcelReader):
def __init__(self, filepath_or_buffer: FilePathOrBuffer):
"""Reader using pyxlsb engine.
"""
Reader using pyxlsb engine.

Parameters
__________
filepath_or_buffer: string, path object, or Workbook
----------
filepath_or_buffer: str, path object, or Workbook
Object to be parsed.
"""
import_optional_dependency("pyxlsb")
Expand All @@ -29,7 +30,7 @@ def _workbook_class(self):
def load_workbook(self, filepath_or_buffer: FilePathOrBuffer):
from pyxlsb import open_workbook

# Todo: hack in buffer capability
# TODO: hack in buffer capability
# This might need some modifications to the Pyxlsb library
# Actual work for opening it is in xlsbpackage.py, line 20-ish

Expand All @@ -48,7 +49,7 @@ def get_sheet_by_index(self, index: int):
return self.book.get_sheet(index + 1)

def _convert_cell(self, cell, convert_float: bool) -> Scalar:
# Todo: there is no way to distinguish between floats and datetimes in pyxlsb
# TODO: there is no way to distinguish between floats and datetimes in pyxlsb
# This means that there is no way to read datetime types from an xlsb file yet
if cell.v is None:
return "" # Prevents non-named columns from not showing up as Unnamed: i
Expand Down
6 changes: 3 additions & 3 deletions pandas/util/_test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ def safe_import(mod_name: str, min_version: Optional[str] = None):


# TODO:
# remove when gh-24839 is fixed; this affects numpy 1.16
# and pytables 3.4.4
# remove when gh-24839 is fixed.
# this affects numpy 1.16 and pytables 3.4.4
tables = safe_import("tables")
xfail_non_writeable = pytest.mark.xfail(
tables
and LooseVersion(np.__version__) >= LooseVersion("1.16")
and LooseVersion(tables.__version__) < LooseVersion("3.5.1"),
reason=(
"gh-25511, gh-24839. pytables needs a "
"release beyong 3.4.4 to support numpy 1.16x"
"release beyond 3.4.4 to support numpy 1.16.x"
),
)

Expand Down