From 146d202e2bb25972c899f01c7f001f93ee6b5ded Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sun, 26 Sep 2021 10:32:32 -0400 Subject: [PATCH 1/7] CLN: remove unused functions --- pandas/_config/config.py | 6 -- pandas/_testing/__init__.py | 49 ----------- pandas/plotting/_matplotlib/converter.py | 22 ----- pandas/util/_depr_module.py | 107 ----------------------- 4 files changed, 184 deletions(-) delete mode 100644 pandas/util/_depr_module.py diff --git a/pandas/_config/config.py b/pandas/_config/config.py index ed48ff7ae08c6..cc3e85f784392 100644 --- a/pandas/_config/config.py +++ b/pandas/_config/config.py @@ -570,12 +570,6 @@ def _get_root(key: str) -> tuple[dict[str, Any], str]: return cursor, path[-1] -def _is_deprecated(key: str) -> bool: - """Returns True if the given option has been deprecated""" - key = key.lower() - return key in _deprecated_options - - def _get_deprecated_option(key: str): """ Retrieves the metadata for a deprecated option, if `key` is deprecated. diff --git a/pandas/_testing/__init__.py b/pandas/_testing/__init__.py index c54185e324646..7446d896f9a24 100644 --- a/pandas/_testing/__init__.py +++ b/pandas/_testing/__init__.py @@ -388,55 +388,6 @@ def makeMultiIndex(k=10, names=None, **kwargs): ] -def _make_timeseries(start="2000-01-01", end="2000-12-31", freq="1D", seed=None): - """ - Make a DataFrame with a DatetimeIndex - - Parameters - ---------- - start : str or Timestamp, default "2000-01-01" - The start of the index. Passed to date_range with `freq`. - end : str or Timestamp, default "2000-12-31" - The end of the index. Passed to date_range with `freq`. - freq : str or Freq - The frequency to use for the DatetimeIndex - seed : int, optional - The random state seed. - - * name : object dtype with string names - * id : int dtype with - * x, y : float dtype - - Examples - -------- - >>> _make_timeseries() - id name x y - timestamp - 2000-01-01 982 Frank 0.031261 0.986727 - 2000-01-02 1025 Edith -0.086358 -0.032920 - 2000-01-03 982 Edith 0.473177 0.298654 - 2000-01-04 1009 Sarah 0.534344 -0.750377 - 2000-01-05 963 Zelda -0.271573 0.054424 - ... ... ... ... ... - 2000-12-27 980 Ingrid -0.132333 -0.422195 - 2000-12-28 972 Frank -0.376007 -0.298687 - 2000-12-29 1009 Ursula -0.865047 -0.503133 - 2000-12-30 1000 Hannah -0.063757 -0.507336 - 2000-12-31 972 Tim -0.869120 0.531685 - """ - index = pd.date_range(start=start, end=end, freq=freq, name="timestamp") - n = len(index) - state = np.random.RandomState(seed) - columns = { - "name": state.choice(_names, size=n), - "id": state.poisson(1000, size=n), - "x": state.rand(n) * 2 - 1, - "y": state.rand(n) * 2 - 1, - } - df = DataFrame(columns, index=index, columns=sorted(columns)) - if df.index[-1] == end: - df = df.iloc[:-1] - return df def index_subclass_makers_generator(): diff --git a/pandas/plotting/_matplotlib/converter.py b/pandas/plotting/_matplotlib/converter.py index ead0a2129d29f..66e328685b749 100644 --- a/pandas/plotting/_matplotlib/converter.py +++ b/pandas/plotting/_matplotlib/converter.py @@ -447,28 +447,6 @@ def autoscale(self): return self.nonsingular(vmin, vmax) -def _from_ordinal(x, tz: tzinfo | None = None) -> datetime: - ix = int(x) - dt = datetime.fromordinal(ix) - remainder = float(x) - ix - hour, remainder = divmod(24 * remainder, 1) - minute, remainder = divmod(60 * remainder, 1) - second, remainder = divmod(60 * remainder, 1) - microsecond = int(1_000_000 * remainder) - if microsecond < 10: - microsecond = 0 # compensate for rounding errors - dt = datetime( - dt.year, dt.month, dt.day, int(hour), int(minute), int(second), microsecond - ) - if tz is not None: - dt = dt.astimezone(tz) - - if microsecond > 999990: # compensate for rounding errors - dt += timedelta(microseconds=1_000_000 - microsecond) - - return dt - - # Fixed frequency dynamic tick locators and formatters # ------------------------------------------------------------------------- diff --git a/pandas/util/_depr_module.py b/pandas/util/_depr_module.py deleted file mode 100644 index 5694ca24aab57..0000000000000 --- a/pandas/util/_depr_module.py +++ /dev/null @@ -1,107 +0,0 @@ -""" -This module houses a utility class for mocking deprecated modules. -It is for internal use only and should not be used beyond this purpose. -""" - -import importlib -from typing import Iterable -import warnings - - -class _DeprecatedModule: - """ - Class for mocking deprecated modules. - - Parameters - ---------- - deprmod : name of module to be deprecated. - deprmodto : name of module as a replacement, optional. - If not given, the __module__ attribute will - be used when needed. - removals : objects or methods in module that will no longer be - accessible once module is removed. - moved : dict, optional - dictionary of function name -> new location for moved - objects - """ - - def __init__(self, deprmod, deprmodto=None, removals=None, moved=None): - self.deprmod = deprmod - self.deprmodto = deprmodto - self.removals = removals - if self.removals is not None: - self.removals = frozenset(self.removals) - self.moved = moved - - # For introspection purposes. - self.self_dir = frozenset(dir(type(self))) - - def __dir__(self) -> Iterable[str]: - deprmodule = self._import_deprmod() - return dir(deprmodule) - - def __repr__(self) -> str: - deprmodule = self._import_deprmod() - return repr(deprmodule) - - __str__ = __repr__ - - def __getattr__(self, name: str): - if name in self.self_dir: - return object.__getattribute__(self, name) - - try: - deprmodule = self._import_deprmod(self.deprmod) - except ImportError: - if self.deprmodto is None: - raise - - # a rename - deprmodule = self._import_deprmod(self.deprmodto) - - obj = getattr(deprmodule, name) - - if self.removals is not None and name in self.removals: - warnings.warn( - f"{self.deprmod}.{name} is deprecated and will be removed in " - "a future version.", - FutureWarning, - stacklevel=2, - ) - elif self.moved is not None and name in self.moved: - warnings.warn( - f"{self.deprmod} is deprecated and will be removed in " - f"a future version.\nYou can access {name} as {self.moved[name]}", - FutureWarning, - stacklevel=2, - ) - else: - deprmodto = self.deprmodto - if deprmodto is False: - warnings.warn( - f"{self.deprmod}.{name} is deprecated and will be removed in " - "a future version.", - FutureWarning, - stacklevel=2, - ) - else: - if deprmodto is None: - deprmodto = obj.__module__ - # The object is actually located in another module. - warnings.warn( - f"{self.deprmod}.{name} is deprecated. Please use " - f"{deprmodto}.{name} instead.", - FutureWarning, - stacklevel=2, - ) - - return obj - - def _import_deprmod(self, mod=None): - if mod is None: - mod = self.deprmod - - with warnings.catch_warnings(): - warnings.filterwarnings("ignore", category=FutureWarning) - deprmodule = importlib.import_module(mod) - return deprmodule From cd8c887e97d583a7e6d84eb11b0ba659ffdcc69c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sun, 26 Sep 2021 10:57:20 -0400 Subject: [PATCH 2/7] unused variables --- pandas/io/clipboard/__init__.py | 10 +++++----- pandas/io/formats/format.py | 2 +- pandas/io/formats/xml.py | 2 -- pandas/io/html.py | 1 - pandas/io/xml.py | 8 ++------ 5 files changed, 8 insertions(+), 15 deletions(-) diff --git a/pandas/io/clipboard/__init__.py b/pandas/io/clipboard/__init__.py index 947deb39ae064..0417529999890 100644 --- a/pandas/io/clipboard/__init__.py +++ b/pandas/io/clipboard/__init__.py @@ -116,7 +116,7 @@ def copy_osx_pbcopy(text): def paste_osx_pbcopy(): p = subprocess.Popen(["pbpaste", "r"], stdout=subprocess.PIPE, close_fds=True) - stdout, stderr = p.communicate() + stdout = p.communicate()[0] return stdout.decode(ENCODING) return copy_osx_pbcopy, paste_osx_pbcopy @@ -194,7 +194,7 @@ def paste_xclip(primary=False): stderr=subprocess.PIPE, close_fds=True, ) - stdout, stderr = p.communicate() + stdout = p.communicate()[0] # Intentionally ignore extraneous output on stderr when clipboard is empty return stdout.decode(ENCODING) @@ -222,7 +222,7 @@ def paste_xsel(primary=False): p = subprocess.Popen( ["xsel", selection_flag, "-o"], stdout=subprocess.PIPE, close_fds=True ) - stdout, stderr = p.communicate() + stdout = p.communicate()[0] return stdout.decode(ENCODING) return copy_xsel, paste_xsel @@ -250,7 +250,7 @@ def paste_klipper(): stdout=subprocess.PIPE, close_fds=True, ) - stdout, stderr = p.communicate() + stdout = p.communicate()[0] # Workaround for https://bugs.kde.org/show_bug.cgi?id=342874 # TODO: https://github.com/asweigart/pyperclip/issues/43 @@ -493,7 +493,7 @@ def paste_wsl(): stderr=subprocess.PIPE, close_fds=True, ) - stdout, stderr = p.communicate() + stdout = p.communicate()[0] # WSL appends "\r\n" to the contents. return stdout[:-2].decode(ENCODING) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index d636838d21d0e..cfda2911db73f 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -876,7 +876,7 @@ def space_format(x, y): need_leadsp = dict(zip(fmt_columns, map(is_numeric_dtype, dtypes))) str_columns = [ [" " + x if not self._get_formatter(i) and need_leadsp[x] else x] - for i, (col, x) in enumerate(zip(columns, fmt_columns)) + for i, x in enumerate(fmt_columns) ] # self.str_columns = str_columns return str_columns diff --git a/pandas/io/formats/xml.py b/pandas/io/formats/xml.py index f5ba8c6b53335..ea7d1dfa1645e 100644 --- a/pandas/io/formats/xml.py +++ b/pandas/io/formats/xml.py @@ -264,8 +264,6 @@ def build_elems(self) -> None: def write_output(self) -> str | None: xml_doc = self.build_tree() - out_str: str | None - if self.path_or_buffer is not None: with get_handle( self.path_or_buffer, diff --git a/pandas/io/html.py b/pandas/io/html.py index 2947b22f85d61..cbf10798a538a 100644 --- a/pandas/io/html.py +++ b/pandas/io/html.py @@ -632,7 +632,6 @@ def _build_xpath_expr(attrs) -> str: _re_namespace = {"re": "http://exslt.org/regular-expressions"} -_valid_schemes = "http", "file", "ftp" class _LxmlFrameParser(_HtmlFrameParser): diff --git a/pandas/io/xml.py b/pandas/io/xml.py index 8b0055a522e25..bc3436861f1a8 100644 --- a/pandas/io/xml.py +++ b/pandas/io/xml.py @@ -297,9 +297,7 @@ def _parse_nodes(self) -> list[dict[str, str | None]]: dicts = [{k: d[k] if k in d.keys() else None for k in keys} for d in dicts] if self.names: - dicts = [ - {nm: v for nm, (k, v) in zip(self.names, d.items())} for d in dicts - ] + dicts = [{nm: v for nm, v in zip(self.names, d.values())} for d in dicts] return dicts @@ -478,9 +476,7 @@ def _parse_nodes(self) -> list[dict[str, str | None]]: dicts = [{k: d[k] if k in d.keys() else None for k in keys} for d in dicts] if self.names: - dicts = [ - {nm: v for nm, (k, v) in zip(self.names, d.items())} for d in dicts - ] + dicts = [{nm: v for nm, v in zip(self.names, d.values())} for d in dicts] return dicts From b510248a1348e920e0148e88d82d83e4efed4d0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sun, 26 Sep 2021 11:08:38 -0400 Subject: [PATCH 3/7] unused imports --- pandas/_libs/hashtable.pyi | 1 - pandas/plotting/_matplotlib/converter.py | 1 - 2 files changed, 2 deletions(-) diff --git a/pandas/_libs/hashtable.pyi b/pandas/_libs/hashtable.pyi index 951703e04d5a3..c6319a1f24435 100644 --- a/pandas/_libs/hashtable.pyi +++ b/pandas/_libs/hashtable.pyi @@ -1,5 +1,4 @@ from typing import ( - Any, Hashable, Literal, ) diff --git a/pandas/plotting/_matplotlib/converter.py b/pandas/plotting/_matplotlib/converter.py index 66e328685b749..f745a14c64296 100644 --- a/pandas/plotting/_matplotlib/converter.py +++ b/pandas/plotting/_matplotlib/converter.py @@ -4,7 +4,6 @@ import datetime as pydt from datetime import ( datetime, - timedelta, tzinfo, ) import functools From e545dca66c5d199fbbd4eebbdf700774d66dc190 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sun, 26 Sep 2021 11:27:53 -0400 Subject: [PATCH 4/7] formatting --- pandas/_testing/__init__.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pandas/_testing/__init__.py b/pandas/_testing/__init__.py index 7446d896f9a24..9ebb9c838d6c9 100644 --- a/pandas/_testing/__init__.py +++ b/pandas/_testing/__init__.py @@ -388,8 +388,6 @@ def makeMultiIndex(k=10, names=None, **kwargs): ] - - def index_subclass_makers_generator(): make_index_funcs = [ makeDateIndex, From adebcd6e1b8a5b887698bab9dac85adcbcfcca4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sun, 26 Sep 2021 17:24:27 -0400 Subject: [PATCH 5/7] used in test --- pandas/_config/config.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pandas/_config/config.py b/pandas/_config/config.py index cc3e85f784392..ed48ff7ae08c6 100644 --- a/pandas/_config/config.py +++ b/pandas/_config/config.py @@ -570,6 +570,12 @@ def _get_root(key: str) -> tuple[dict[str, Any], str]: return cursor, path[-1] +def _is_deprecated(key: str) -> bool: + """Returns True if the given option has been deprecated""" + key = key.lower() + return key in _deprecated_options + + def _get_deprecated_option(key: str): """ Retrieves the metadata for a deprecated option, if `key` is deprecated. From af9d567df4f6fe82edcd192ba98a9e3e2f18e2fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sun, 26 Sep 2021 17:52:05 -0400 Subject: [PATCH 6/7] also used in tests --- pandas/plotting/_matplotlib/converter.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pandas/plotting/_matplotlib/converter.py b/pandas/plotting/_matplotlib/converter.py index f745a14c64296..ead0a2129d29f 100644 --- a/pandas/plotting/_matplotlib/converter.py +++ b/pandas/plotting/_matplotlib/converter.py @@ -4,6 +4,7 @@ import datetime as pydt from datetime import ( datetime, + timedelta, tzinfo, ) import functools @@ -446,6 +447,28 @@ def autoscale(self): return self.nonsingular(vmin, vmax) +def _from_ordinal(x, tz: tzinfo | None = None) -> datetime: + ix = int(x) + dt = datetime.fromordinal(ix) + remainder = float(x) - ix + hour, remainder = divmod(24 * remainder, 1) + minute, remainder = divmod(60 * remainder, 1) + second, remainder = divmod(60 * remainder, 1) + microsecond = int(1_000_000 * remainder) + if microsecond < 10: + microsecond = 0 # compensate for rounding errors + dt = datetime( + dt.year, dt.month, dt.day, int(hour), int(minute), int(second), microsecond + ) + if tz is not None: + dt = dt.astimezone(tz) + + if microsecond > 999990: # compensate for rounding errors + dt += timedelta(microseconds=1_000_000 - microsecond) + + return dt + + # Fixed frequency dynamic tick locators and formatters # ------------------------------------------------------------------------- From 848a259d575e614983c74fa1b764879a4441fe01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Sun, 26 Sep 2021 18:47:36 -0400 Subject: [PATCH 7/7] used in docs --- pandas/_testing/__init__.py | 51 +++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/pandas/_testing/__init__.py b/pandas/_testing/__init__.py index 9ebb9c838d6c9..c54185e324646 100644 --- a/pandas/_testing/__init__.py +++ b/pandas/_testing/__init__.py @@ -388,6 +388,57 @@ def makeMultiIndex(k=10, names=None, **kwargs): ] +def _make_timeseries(start="2000-01-01", end="2000-12-31", freq="1D", seed=None): + """ + Make a DataFrame with a DatetimeIndex + + Parameters + ---------- + start : str or Timestamp, default "2000-01-01" + The start of the index. Passed to date_range with `freq`. + end : str or Timestamp, default "2000-12-31" + The end of the index. Passed to date_range with `freq`. + freq : str or Freq + The frequency to use for the DatetimeIndex + seed : int, optional + The random state seed. + + * name : object dtype with string names + * id : int dtype with + * x, y : float dtype + + Examples + -------- + >>> _make_timeseries() + id name x y + timestamp + 2000-01-01 982 Frank 0.031261 0.986727 + 2000-01-02 1025 Edith -0.086358 -0.032920 + 2000-01-03 982 Edith 0.473177 0.298654 + 2000-01-04 1009 Sarah 0.534344 -0.750377 + 2000-01-05 963 Zelda -0.271573 0.054424 + ... ... ... ... ... + 2000-12-27 980 Ingrid -0.132333 -0.422195 + 2000-12-28 972 Frank -0.376007 -0.298687 + 2000-12-29 1009 Ursula -0.865047 -0.503133 + 2000-12-30 1000 Hannah -0.063757 -0.507336 + 2000-12-31 972 Tim -0.869120 0.531685 + """ + index = pd.date_range(start=start, end=end, freq=freq, name="timestamp") + n = len(index) + state = np.random.RandomState(seed) + columns = { + "name": state.choice(_names, size=n), + "id": state.poisson(1000, size=n), + "x": state.rand(n) * 2 - 1, + "y": state.rand(n) * 2 - 1, + } + df = DataFrame(columns, index=index, columns=sorted(columns)) + if df.index[-1] == end: + df = df.iloc[:-1] + return df + + def index_subclass_makers_generator(): make_index_funcs = [ makeDateIndex,