From bfd258c76fb790376935aef77d9eb02b209dac34 Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Sun, 21 Jun 2020 22:16:29 +0100 Subject: [PATCH] Fix language style --- asv_bench/benchmarks/groupby.py | 4 +- ci/code_checks.sh | 6 +-- doc/source/whatsnew/v0.14.1.rst | 2 +- pandas/core/groupby/base.py | 14 +++--- pandas/core/groupby/generic.py | 20 ++++---- pandas/core/groupby/groupby.py | 8 ++-- .../{test_whitelist.py => test_allowlist.py} | 48 +++++++++---------- .../tests/groupby/transform/test_transform.py | 2 +- 8 files changed, 52 insertions(+), 52 deletions(-) rename pandas/tests/groupby/{test_whitelist.py => test_allowlist.py} (90%) diff --git a/asv_bench/benchmarks/groupby.py b/asv_bench/benchmarks/groupby.py index c9ac275cc4ea7..5ffda03fad80f 100644 --- a/asv_bench/benchmarks/groupby.py +++ b/asv_bench/benchmarks/groupby.py @@ -16,7 +16,7 @@ from .pandas_vb_common import tm -method_blacklist = { +method_blocklist = { "object": { "median", "prod", @@ -403,7 +403,7 @@ class GroupByMethods: ] def setup(self, dtype, method, application): - if method in method_blacklist.get(dtype, {}): + if method in method_blocklist.get(dtype, {}): raise NotImplementedError # skip benchmark ngroups = 1000 size = ngroups * 2 diff --git a/ci/code_checks.sh b/ci/code_checks.sh index f7a513ca22d53..7b12de387d648 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -248,19 +248,19 @@ fi ### CODE ### if [[ -z "$CHECK" || "$CHECK" == "code" ]]; then - MSG='Check import. No warnings, and blacklist some optional dependencies' ; echo $MSG + MSG='Check import. No warnings, and blocklist some optional dependencies' ; echo $MSG python -W error -c " import sys import pandas -blacklist = {'bs4', 'gcsfs', 'html5lib', 'http', 'ipython', 'jinja2', 'hypothesis', +blocklist = {'bs4', 'gcsfs', 'html5lib', 'http', 'ipython', 'jinja2', 'hypothesis', 'lxml', 'matplotlib', 'numexpr', 'openpyxl', 'py', 'pytest', 's3fs', 'scipy', 'tables', 'urllib.request', 'xlrd', 'xlsxwriter', 'xlwt'} # GH#28227 for some of these check for top-level modules, while others are # more specific (e.g. urllib.request) import_mods = set(m.split('.')[0] for m in sys.modules) | set(sys.modules) -mods = blacklist & import_mods +mods = blocklist & import_mods if mods: sys.stderr.write('err: pandas should not import: {}\n'.format(', '.join(mods))) sys.exit(len(mods)) diff --git a/doc/source/whatsnew/v0.14.1.rst b/doc/source/whatsnew/v0.14.1.rst index 3dfc4272681df..5de193007474c 100644 --- a/doc/source/whatsnew/v0.14.1.rst +++ b/doc/source/whatsnew/v0.14.1.rst @@ -131,7 +131,7 @@ Enhancements - Implemented ``sem`` (standard error of the mean) operation for ``Series``, ``DataFrame``, ``Panel``, and ``Groupby`` (:issue:`6897`) -- Add ``nlargest`` and ``nsmallest`` to the ``Series`` ``groupby`` whitelist, +- Add ``nlargest`` and ``nsmallest`` to the ``Series`` ``groupby`` allowlist, which means you can now use these methods on a ``SeriesGroupBy`` object (:issue:`7053`). - All offsets ``apply``, ``rollforward`` and ``rollback`` can now handle ``np.datetime64``, previously results in ``ApplyTypeError`` (:issue:`7452`) diff --git a/pandas/core/groupby/base.py b/pandas/core/groupby/base.py index 08352d737dee0..e71b2f94c8014 100644 --- a/pandas/core/groupby/base.py +++ b/pandas/core/groupby/base.py @@ -1,6 +1,6 @@ """ Provide basic components for groupby. These definitions -hold the whitelist of methods that are exposed on the +hold the allowlist of methods that are exposed on the SeriesGroupBy and the DataFrameGroupBy objects. """ import collections @@ -53,7 +53,7 @@ def _gotitem(self, key, ndim, subset=None): # forwarding methods from NDFrames plotting_methods = frozenset(["plot", "hist"]) -common_apply_whitelist = ( +common_apply_allowlist = ( frozenset( [ "quantile", @@ -72,9 +72,9 @@ def _gotitem(self, key, ndim, subset=None): | plotting_methods ) -series_apply_whitelist = ( +series_apply_allowlist = ( ( - common_apply_whitelist + common_apply_allowlist | { "nlargest", "nsmallest", @@ -84,13 +84,13 @@ def _gotitem(self, key, ndim, subset=None): ) ) | frozenset(["dtype", "unique"]) -dataframe_apply_whitelist = common_apply_whitelist | frozenset(["dtypes", "corrwith"]) +dataframe_apply_allowlist = common_apply_allowlist | frozenset(["dtypes", "corrwith"]) # cythonized transformations or canned "agg+broadcast", which do not # require postprocessing of the result by transform. cythonized_kernels = frozenset(["cumprod", "cumsum", "shift", "cummin", "cummax"]) -cython_cast_blacklist = frozenset(["rank", "count", "size", "idxmin", "idxmax"]) +cython_cast_blocklist = frozenset(["rank", "count", "size", "idxmin", "idxmax"]) # List of aggregation/reduction functions. # These map each group to a single numeric value @@ -186,4 +186,4 @@ def _gotitem(self, key, ndim, subset=None): # Valid values of `name` for `groupby.transform(name)` # NOTE: do NOT edit this directly. New additions should be inserted # into the appropriate list above. -transform_kernel_whitelist = reduction_kernels | transformation_kernels +transform_kernel_allowlist = reduction_kernels | transformation_kernels diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index bc5cf595e49f9..dab8475d9580c 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -121,15 +121,15 @@ def prop(self): return property(prop) -def pin_whitelisted_properties(klass: Type[FrameOrSeries], whitelist: FrozenSet[str]): +def pin_allowlisted_properties(klass: Type[FrameOrSeries], allowlist: FrozenSet[str]): """ - Create GroupBy member defs for DataFrame/Series names in a whitelist. + Create GroupBy member defs for DataFrame/Series names in a allowlist. Parameters ---------- klass : DataFrame or Series class class where members are defined. - whitelist : frozenset[str] + allowlist : frozenset[str] Set of names of klass methods to be constructed Returns @@ -143,7 +143,7 @@ class decorator """ def pinner(cls): - for name in whitelist: + for name in allowlist: if hasattr(cls, name): # don't override anything that was explicitly defined # in the base class @@ -157,9 +157,9 @@ def pinner(cls): return pinner -@pin_whitelisted_properties(Series, base.series_apply_whitelist) +@pin_allowlisted_properties(Series, base.series_apply_allowlist) class SeriesGroupBy(GroupBy[Series]): - _apply_whitelist = base.series_apply_whitelist + _apply_allowlist = base.series_apply_allowlist def _iterate_slices(self) -> Iterable[Series]: yield self._selected_obj @@ -473,7 +473,7 @@ def transform(self, func, *args, engine="cython", engine_kwargs=None, **kwargs): func, *args, engine=engine, engine_kwargs=engine_kwargs, **kwargs ) - elif func not in base.transform_kernel_whitelist: + elif func not in base.transform_kernel_allowlist: msg = f"'{func}' is not a valid function name for transform(name)" raise ValueError(msg) elif func in base.cythonized_kernels: @@ -835,10 +835,10 @@ def pct_change(self, periods=1, fill_method="pad", limit=None, freq=None): return (filled / shifted) - 1 -@pin_whitelisted_properties(DataFrame, base.dataframe_apply_whitelist) +@pin_allowlisted_properties(DataFrame, base.dataframe_apply_allowlist) class DataFrameGroupBy(GroupBy[DataFrame]): - _apply_whitelist = base.dataframe_apply_whitelist + _apply_allowlist = base.dataframe_apply_allowlist _agg_examples_doc = dedent( """ @@ -1456,7 +1456,7 @@ def transform(self, func, *args, engine="cython", engine_kwargs=None, **kwargs): func, *args, engine=engine, engine_kwargs=engine_kwargs, **kwargs ) - elif func not in base.transform_kernel_whitelist: + elif func not in base.transform_kernel_allowlist: msg = f"'{func}' is not a valid function name for transform(name)" raise ValueError(msg) elif func in base.cythonized_kernels: diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 02f7f605a7605..d039b715b3c08 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -475,7 +475,7 @@ def _group_selection_context(groupby): class _GroupBy(PandasObject, SelectionMixin, Generic[FrameOrSeries]): _group_selection = None - _apply_whitelist: FrozenSet[str] = frozenset() + _apply_allowlist: FrozenSet[str] = frozenset() def __init__( self, @@ -689,7 +689,7 @@ def _set_result_index_ordered(self, result): return result def _dir_additions(self): - return self.obj._dir_additions() | self._apply_whitelist + return self.obj._dir_additions() | self._apply_allowlist def __getattr__(self, attr: str): if attr in self._internal_names_set: @@ -729,7 +729,7 @@ def pipe(self, func, *args, **kwargs): plot = property(GroupByPlot) def _make_wrapper(self, name): - assert name in self._apply_whitelist + assert name in self._apply_allowlist self._set_group_selection() @@ -944,7 +944,7 @@ def _transform_should_cast(self, func_nm: str) -> bool: """ filled_series = self.grouper.size().fillna(0) assert filled_series is not None - return filled_series.gt(0).any() and func_nm not in base.cython_cast_blacklist + return filled_series.gt(0).any() and func_nm not in base.cython_cast_blocklist def _cython_transform(self, how: str, numeric_only: bool = True, **kwargs): output: Dict[base.OutputKey, np.ndarray] = {} diff --git a/pandas/tests/groupby/test_whitelist.py b/pandas/tests/groupby/test_allowlist.py similarity index 90% rename from pandas/tests/groupby/test_whitelist.py rename to pandas/tests/groupby/test_allowlist.py index 9b595328d9230..0fd66cc047017 100644 --- a/pandas/tests/groupby/test_whitelist.py +++ b/pandas/tests/groupby/test_allowlist.py @@ -31,7 +31,7 @@ ] AGG_FUNCTIONS_WITH_SKIPNA = ["skew", "mad"] -df_whitelist = [ +df_allowlist = [ "quantile", "fillna", "mad", @@ -50,12 +50,12 @@ ] -@pytest.fixture(params=df_whitelist) -def df_whitelist_fixture(request): +@pytest.fixture(params=df_allowlist) +def df_allowlist_fixture(request): return request.param -s_whitelist = [ +s_allowlist = [ "quantile", "fillna", "mad", @@ -78,8 +78,8 @@ def df_whitelist_fixture(request): ] -@pytest.fixture(params=s_whitelist) -def s_whitelist_fixture(request): +@pytest.fixture(params=s_allowlist) +def s_allowlist_fixture(request): return request.param @@ -119,10 +119,10 @@ def df_letters(): return df -@pytest.mark.parametrize("whitelist", [df_whitelist, s_whitelist]) -def test_groupby_whitelist(df_letters, whitelist): +@pytest.mark.parametrize("allowlist", [df_allowlist, s_allowlist]) +def test_groupby_allowlist(df_letters, allowlist): df = df_letters - if whitelist == df_whitelist: + if allowlist == df_allowlist: # dataframe obj = df_letters else: @@ -130,11 +130,11 @@ def test_groupby_whitelist(df_letters, whitelist): gb = obj.groupby(df.letters) - assert set(whitelist) == set(gb._apply_whitelist) + assert set(allowlist) == set(gb._apply_allowlist) -def check_whitelist(obj, df, m): - # check the obj for a particular whitelist m +def check_allowlist(obj, df, m): + # check the obj for a particular allowlist m gb = obj.groupby(df.letters) @@ -155,16 +155,16 @@ def check_whitelist(obj, df, m): assert n.endswith(m) -def test_groupby_series_whitelist(df_letters, s_whitelist_fixture): - m = s_whitelist_fixture +def test_groupby_series_allowlist(df_letters, s_allowlist_fixture): + m = s_allowlist_fixture df = df_letters - check_whitelist(df.letters, df, m) + check_allowlist(df.letters, df, m) -def test_groupby_frame_whitelist(df_letters, df_whitelist_fixture): - m = df_whitelist_fixture +def test_groupby_frame_allowlist(df_letters, df_allowlist_fixture): + m = df_allowlist_fixture df = df_letters - check_whitelist(df, df, m) + check_allowlist(df, df, m) @pytest.fixture @@ -187,10 +187,10 @@ def raw_frame(): @pytest.mark.parametrize("axis", [0, 1]) @pytest.mark.parametrize("skipna", [True, False]) @pytest.mark.parametrize("sort", [True, False]) -def test_regression_whitelist_methods(raw_frame, op, level, axis, skipna, sort): +def test_regression_allowlist_methods(raw_frame, op, level, axis, skipna, sort): # GH6944 # GH 17537 - # explicitly test the whitelist methods + # explicitly test the allowlist methods if axis == 0: frame = raw_frame @@ -213,11 +213,11 @@ def test_regression_whitelist_methods(raw_frame, op, level, axis, skipna, sort): tm.assert_frame_equal(result, expected) -def test_groupby_blacklist(df_letters): +def test_groupby_blocklist(df_letters): df = df_letters s = df_letters.floats - blacklist = [ + blocklist = [ "eval", "query", "abs", @@ -234,9 +234,9 @@ def test_groupby_blacklist(df_letters): ] to_methods = [method for method in dir(df) if method.startswith("to_")] - blacklist.extend(to_methods) + blocklist.extend(to_methods) - for bl in blacklist: + for bl in blocklist: for obj in (df, s): gb = obj.groupby(df.letters) diff --git a/pandas/tests/groupby/transform/test_transform.py b/pandas/tests/groupby/transform/test_transform.py index fd4ee2a81ebd8..cdaf27e214d80 100644 --- a/pandas/tests/groupby/transform/test_transform.py +++ b/pandas/tests/groupby/transform/test_transform.py @@ -728,7 +728,7 @@ def test_cython_transform_frame(op, args, targop): # dict(by=['int','string'])]: gb = df.groupby(**gb_target) - # whitelisted methods set the selection before applying + # allowlisted methods set the selection before applying # bit a of hack to make sure the cythonized shift # is equivalent to pre 0.17.1 behavior if op == "shift":