From b7a8bfd45de552de09c4a498d09b28963e19e7d2 Mon Sep 17 00:00:00 2001 From: Dushimirimana Salomon Date: Wed, 11 Aug 2021 02:45:10 -0500 Subject: [PATCH 1/5] DOC: updates documentations Closes #21749 --- pandas/_testing/_io.py | 11 +++++------ pandas/_testing/asserters.py | 20 ++++++++++---------- pandas/core/algorithms.py | 8 ++++---- pandas/core/arrays/sparse/array.py | 4 ++-- pandas/core/dtypes/base.py | 7 +++---- pandas/core/dtypes/common.py | 8 ++++---- pandas/core/dtypes/concat.py | 20 ++++++++++---------- pandas/core/dtypes/inference.py | 12 ++++++------ pandas/core/generic.py | 8 ++++---- pandas/io/formats/latex.py | 12 ++++++------ pandas/io/stata.py | 16 ++++++++-------- pandas/tseries/holiday.py | 14 +++++++------- 12 files changed, 69 insertions(+), 71 deletions(-) diff --git a/pandas/_testing/_io.py b/pandas/_testing/_io.py index 58ce9b17909bb..0cdc8ac73ab52 100644 --- a/pandas/_testing/_io.py +++ b/pandas/_testing/_io.py @@ -160,11 +160,10 @@ def network( Tests decorated with @network will fail if it's possible to make a network connection to another URL (defaults to google.com):: - >>> from pandas._testing import network - >>> from pandas.io.common import urlopen - >>> @network + >>> import pandas as pd + >>> @pd._testing.network ... def test_network(): - ... with urlopen("rabbit://bonanza.com"): + ... with pd.io.common.urlopen("rabbit://bonanza.com"): ... pass Traceback ... @@ -172,7 +171,7 @@ def network( You can specify alternative URLs:: - >>> @network("https://www.yahoo.com") + >>> @pd._testing.network("https://www.yahoo.com") ... def test_something_with_yahoo(): ... raise IOError("Failure Message") >>> test_something_with_yahoo() @@ -183,7 +182,7 @@ def network( If you set check_before_test, it will check the url first and not run the test on failure:: - >>> @network("failing://url.blaher", check_before_test=True) + >>> @pd._testing.network("failing://url.blaher", check_before_test=True) ... def test_something(): ... print("I ran!") ... raise ValueError("Failure") diff --git a/pandas/_testing/asserters.py b/pandas/_testing/asserters.py index d0957b1814213..17da8c7a4d3db 100644 --- a/pandas/_testing/asserters.py +++ b/pandas/_testing/asserters.py @@ -303,10 +303,10 @@ def assert_index_equal( Examples -------- - >>> from pandas.testing import assert_index_equal + >>> import pandas as pd >>> a = pd.Index([1, 2, 3]) >>> b = pd.Index([1, 2, 3]) - >>> assert_index_equal(a, b) + >>> pd.testing.assert_index_equal(a, b) """ __tracebackhide__ = True @@ -794,10 +794,10 @@ def assert_extension_array_equal( Examples -------- - >>> from pandas.testing import assert_extension_array_equal + >>> import pandas as pd >>> a = pd.Series([1, 2, 3, 4]) >>> b, c = a.array, a.array - >>> assert_extension_array_equal(b, c) + >>> pd.testing.assert_extension_array_equal(b, c) """ if check_less_precise is not no_default: warnings.warn( @@ -938,10 +938,10 @@ def assert_series_equal( Examples -------- - >>> from pandas.testing import assert_series_equal + >>> import pandas as pd >>> a = pd.Series([1, 2, 3, 4]) >>> b = pd.Series([1, 2, 3, 4]) - >>> assert_series_equal(a, b) + >>> pd.testing.assert_series_equal(a, b) """ __tracebackhide__ = True @@ -1203,17 +1203,17 @@ def assert_frame_equal( This example shows comparing two DataFrames that are equal but with columns of differing dtypes. - >>> from pandas._testing import assert_frame_equal + >>> import pandas as pd >>> df1 = pd.DataFrame({'a': [1, 2], 'b': [3, 4]}) >>> df2 = pd.DataFrame({'a': [1, 2], 'b': [3.0, 4.0]}) df1 equals itself. - >>> assert_frame_equal(df1, df1) + >>> pd._testing.assert_frame_equal(df1, df1) df1 differs from df2 as column 'b' is of a different type. - >>> assert_frame_equal(df1, df2) + >>> pd._testing.assert_frame_equal(df1, df2) Traceback (most recent call last): ... AssertionError: Attributes of DataFrame.iloc[:, 1] (column name="b") are different @@ -1224,7 +1224,7 @@ def assert_frame_equal( Ignore differing dtypes in columns with check_dtype. - >>> assert_frame_equal(df1, df2, check_dtype=False) + >>> pd._testing.assert_frame_equal(df1, df2, check_dtype=False) """ __tracebackhide__ = True diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 52c5790e1fc24..3421c5b5b01a6 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -1462,20 +1462,20 @@ def take( Examples -------- - >>> from pandas.api.extensions import take + >>> import pandas as pd With the default ``allow_fill=False``, negative numbers indicate positional indices from the right. - >>> take(np.array([10, 20, 30]), [0, 0, -1]) + >>> pd.api.extensions.take(np.array([10, 20, 30]), [0, 0, -1]) array([10, 10, 30]) Setting ``allow_fill=True`` will place `fill_value` in those positions. - >>> take(np.array([10, 20, 30]), [0, 0, -1], allow_fill=True) + >>> pd.api.extensions.take(np.array([10, 20, 30]), [0, 0, -1], allow_fill=True) array([10., 10., nan]) - >>> take(np.array([10, 20, 30]), [0, 0, -1], allow_fill=True, + >>> pd.api.extensions.take(np.array([10, 20, 30]), [0, 0, -1], allow_fill=True, ... fill_value=-10) array([ 10, 10, -10]) """ diff --git a/pandas/core/arrays/sparse/array.py b/pandas/core/arrays/sparse/array.py index b1cfcbd69a30b..6664880c897fc 100644 --- a/pandas/core/arrays/sparse/array.py +++ b/pandas/core/arrays/sparse/array.py @@ -282,8 +282,8 @@ class SparseArray(OpsMixin, PandasObject, ExtensionArray): Examples -------- - >>> from pandas.arrays import SparseArray - >>> arr = SparseArray([0, 0, 1, 2]) + >>> import pandas as pd + >>> arr = pd.arrays.SparseArray([0, 0, 1, 2]) >>> arr [0, 0, 1, 2] Fill: 0 diff --git a/pandas/core/dtypes/base.py b/pandas/core/dtypes/base.py index 7dad8c61f4fc7..0f727516c2043 100644 --- a/pandas/core/dtypes/base.py +++ b/pandas/core/dtypes/base.py @@ -387,10 +387,9 @@ def register_extension_dtype(cls: type_t[ExtensionDtypeT]) -> type_t[ExtensionDt Examples -------- - >>> from pandas.api.extensions import register_extension_dtype - >>> from pandas.api.extensions import ExtensionDtype - >>> @register_extension_dtype - ... class MyExtensionDtype(ExtensionDtype): + >>> import pandas as pd + >>> @pd.api.extensions.register_extension_dtype + ... class MyExtensionDtype(pd.api.extensions.ExtensionDtype): ... name = "myextension" """ _registry.register(cls) diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index 3a870c2287584..579994170bb12 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -1450,15 +1450,15 @@ def is_extension_array_dtype(arr_or_dtype) -> bool: Examples -------- - >>> from pandas.api.types import is_extension_array_dtype + >>> import pandas as pd >>> arr = pd.Categorical(['a', 'b']) - >>> is_extension_array_dtype(arr) + >>> pd.api.types.is_extension_array_dtype(arr) True - >>> is_extension_array_dtype(arr.dtype) + >>> pd.api.types.is_extension_array_dtype(arr.dtype) True >>> arr = np.array(['a', 'b']) - >>> is_extension_array_dtype(arr.dtype) + >>> pd.api.types.is_extension_array_dtype(arr.dtype) False """ dtype = getattr(arr_or_dtype, "dtype", arr_or_dtype) diff --git a/pandas/core/dtypes/concat.py b/pandas/core/dtypes/concat.py index b0d00775bbed1..f5442a451ae9b 100644 --- a/pandas/core/dtypes/concat.py +++ b/pandas/core/dtypes/concat.py @@ -191,16 +191,16 @@ def union_categoricals( Examples -------- - >>> from pandas.api.types import union_categoricals + >>> import pandas as pd If you want to combine categoricals that do not necessarily have - the same categories, `union_categoricals` will combine a list-like + the same categories, `pd.api.types.union_categoricals` will combine a list-like of categoricals. The new categories will be the union of the categories being combined. >>> a = pd.Categorical(["b", "c"]) >>> b = pd.Categorical(["a", "b"]) - >>> union_categoricals([a, b]) + >>> pd.api.types.union_categoricals([a, b]) ['b', 'c', 'a', 'b'] Categories (3, object): ['b', 'c', 'a'] @@ -208,17 +208,17 @@ def union_categoricals( in the `categories` of the data. If you want the categories to be lexsorted, use `sort_categories=True` argument. - >>> union_categoricals([a, b], sort_categories=True) + >>> pd.api.types.union_categoricals([a, b], sort_categories=True) ['b', 'c', 'a', 'b'] Categories (3, object): ['a', 'b', 'c'] - `union_categoricals` also works with the case of combining two + `pd.api.types.union_categoricals` also works with the case of combining two categoricals of the same categories and order information (e.g. what you could also `append` for). >>> a = pd.Categorical(["a", "b"], ordered=True) >>> b = pd.Categorical(["a", "b", "a"], ordered=True) - >>> union_categoricals([a, b]) + >>> pd.api.types.union_categoricals([a, b]) ['a', 'b', 'a', 'b', 'a'] Categories (2, object): ['a' < 'b'] @@ -226,7 +226,7 @@ def union_categoricals( >>> a = pd.Categorical(["a", "b"], ordered=True) >>> b = pd.Categorical(["a", "b", "c"], ordered=True) - >>> union_categoricals([a, b]) + >>> pd.api.types.union_categoricals([a, b]) Traceback (most recent call last): ... TypeError: to union ordered Categoricals, all categories must be the same @@ -238,17 +238,17 @@ def union_categoricals( >>> a = pd.Categorical(["a", "b", "c"], ordered=True) >>> b = pd.Categorical(["c", "b", "a"], ordered=True) - >>> union_categoricals([a, b], ignore_order=True) + >>> pd.api.types.union_categoricals([a, b], ignore_order=True) ['a', 'b', 'c', 'c', 'b', 'a'] Categories (3, object): ['a', 'b', 'c'] - `union_categoricals` also works with a `CategoricalIndex`, or `Series` + `pd.api.types.union_categoricals` also works with a `CategoricalIndex`, or `Series` containing categorical data, but note that the resulting array will always be a plain `Categorical` >>> a = pd.Series(["b", "c"], dtype='category') >>> b = pd.Series(["a", "b"], dtype='category') - >>> union_categoricals([a, b]) + >>> pd.api.types.union_categoricals([a, b]) ['b', 'c', 'a', 'b'] Categories (3, object): ['b', 'c', 'a'] """ diff --git a/pandas/core/dtypes/inference.py b/pandas/core/dtypes/inference.py index 1360b66e77dc0..5b395aca8b919 100644 --- a/pandas/core/dtypes/inference.py +++ b/pandas/core/dtypes/inference.py @@ -51,20 +51,20 @@ def is_number(obj) -> bool: Examples -------- - >>> from pandas.api.types import is_number - >>> is_number(1) + >>> import pandas as pd + >>> pd.api.types.is_number(1) True - >>> is_number(7.15) + >>> pd.api.types.is_number(7.15) True Booleans are valid because they are int subclass. - >>> is_number(False) + >>> pd.api.types.is_number(False) True - >>> is_number("foo") + >>> pd.api.types.is_number("foo") False - >>> is_number("5") + >>> pd.api.types.is_number("5") False """ return isinstance(obj, (Number, np.number)) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index b2a4fecbf084d..2e799557a1280 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5681,8 +5681,8 @@ def astype( Convert to ordered categorical type with custom ordering: - >>> from pandas.api.types import CategoricalDtype - >>> cat_dtype = CategoricalDtype( + >>> import pandas as pd + >>> cat_dtype = pd.api.types.CategoricalDtype( ... categories=[2, 1], ordered=True) >>> ser.astype(cat_dtype) 0 1 @@ -8042,10 +8042,10 @@ def resample( To replace the use of the deprecated `loffset` argument: - >>> from pandas.tseries.frequencies import to_offset + >>> import pandas as pd >>> loffset = '19min' >>> ts_out = ts.resample('17min').sum() - >>> ts_out.index = ts_out.index + to_offset(loffset) + >>> ts_out.index = ts_out.index + pd.tseries.frequencies.to_offset(loffset) >>> ts_out 2000-10-01 23:33:00 0 2000-10-01 23:50:00 9 diff --git a/pandas/io/formats/latex.py b/pandas/io/formats/latex.py index 93069a1e2955d..3ef843ac83009 100644 --- a/pandas/io/formats/latex.py +++ b/pandas/io/formats/latex.py @@ -488,9 +488,9 @@ def _select_iterator(self, over: str) -> type[RowStringIterator]: class LongTableBuilder(GenericTableBuilder): """Concrete table builder for longtable. - >>> from pandas import DataFrame + >>> import pandas as pd >>> from pandas.io.formats import format as fmt - >>> df = DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) + >>> df = pd.DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) >>> formatter = fmt.DataFrameFormatter(df) >>> builder = LongTableBuilder(formatter, caption='a long table', ... label='tab:long', column_format='lrl') @@ -578,9 +578,9 @@ def env_end(self) -> str: class RegularTableBuilder(GenericTableBuilder): """Concrete table builder for regular table. - >>> from pandas import DataFrame + >>> import pandas as pd >>> from pandas.io.formats import format as fmt - >>> df = DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) + >>> df = pd.DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) >>> formatter = fmt.DataFrameFormatter(df) >>> builder = RegularTableBuilder(formatter, caption='caption', label='lab', ... column_format='lrc') @@ -625,9 +625,9 @@ def env_end(self) -> str: class TabularBuilder(GenericTableBuilder): """Concrete table builder for tabular environment. - >>> from pandas import DataFrame + >>> import pandas as pd >>> from pandas.io.formats import format as fmt - >>> df = DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) + >>> df = pd.DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) >>> formatter = fmt.DataFrameFormatter(df) >>> builder = TabularBuilder(formatter, column_format='lrc') >>> table = builder.get_result() diff --git a/pandas/io/stata.py b/pandas/io/stata.py index 1f87a017f31e4..b85b5092ae99b 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -3073,20 +3073,20 @@ class StataWriter117(StataWriter): Examples -------- - >>> from pandas.io.stata import StataWriter117 + >>> import pandas as pd >>> data = pd.DataFrame([[1.0, 1, 'a']], columns=['a', 'b', 'c']) - >>> writer = StataWriter117('./data_file.dta', data) + >>> writer = pd.io.stata.StataWriter117('./data_file.dta', data) >>> writer.write_file() Directly write a zip file >>> compression = {"method": "zip", "archive_name": "data_file.dta"} - >>> writer = StataWriter117('./data_file.zip', data, compression=compression) + >>> writer = pd.io.stata.StataWriter117('./data_file.zip', data, compression=compression) >>> writer.write_file() Or with long strings stored in strl format >>> data = pd.DataFrame([['A relatively long string'], [''], ['']], ... columns=['strls']) - >>> writer = StataWriter117('./data_file_with_long_strings.dta', data, + >>> writer = pd.io.stata.StataWriter117('./data_file_with_long_strings.dta', data, ... convert_strl=['strls']) >>> writer.write_file() """ @@ -3465,21 +3465,21 @@ class StataWriterUTF8(StataWriter117): -------- Using Unicode data and column names - >>> from pandas.io.stata import StataWriterUTF8 + >>> import pandas as pd >>> data = pd.DataFrame([[1.0, 1, 'ᴬ']], columns=['a', 'β', 'ĉ']) - >>> writer = StataWriterUTF8('./data_file.dta', data) + >>> writer = pd.io.stata.StataWriterUTF8('./data_file.dta', data) >>> writer.write_file() Directly write a zip file >>> compression = {"method": "zip", "archive_name": "data_file.dta"} - >>> writer = StataWriterUTF8('./data_file.zip', data, compression=compression) + >>> writer = pd.io.stata.StataWriterUTF8('./data_file.zip', data, compression=compression) >>> writer.write_file() Or with long strings stored in strl format >>> data = pd.DataFrame([['ᴀ relatively long ŝtring'], [''], ['']], ... columns=['strls']) - >>> writer = StataWriterUTF8('./data_file_with_long_strings.dta', data, + >>> writer = pd.io.stata.StataWriterUTF8('./data_file_with_long_strings.dta', data, ... convert_strl=['strls']) >>> writer.write_file() """ diff --git a/pandas/tseries/holiday.py b/pandas/tseries/holiday.py index 54ac116afe3cf..caa87a63625e6 100644 --- a/pandas/tseries/holiday.py +++ b/pandas/tseries/holiday.py @@ -177,34 +177,34 @@ class from pandas.tseries.offsets Examples -------- - >>> from pandas.tseries.holiday import Holiday, nearest_workday + >>> import pandas as pd >>> from dateutil.relativedelta import MO - >>> USMemorialDay = Holiday( + >>> USMemorialDay = pd.tseries.holiday.Holiday( ... "Memorial Day", month=5, day=31, offset=pd.DateOffset(weekday=MO(-1)) ... ) >>> USMemorialDay Holiday: Memorial Day (month=5, day=31, offset=) - >>> USLaborDay = Holiday( + >>> USLaborDay = pd.tseries.holiday.Holiday( ... "Labor Day", month=9, day=1, offset=pd.DateOffset(weekday=MO(1)) ... ) >>> USLaborDay Holiday: Labor Day (month=9, day=1, offset=) - >>> July3rd = Holiday("July 3rd", month=7, day=3) + >>> July3rd = pd.tseries.holiday.Holiday("July 3rd", month=7, day=3) >>> July3rd Holiday: July 3rd (month=7, day=3, ) - >>> NewYears = Holiday( - ... "New Years Day", month=1, day=1, observance=nearest_workday + >>> NewYears = pd.tseries.holiday.Holiday( + ... "New Years Day", month=1, day=1, observance=pd.tseries.holiday.nearest_workday ... ) >>> NewYears # doctest: +SKIP Holiday: New Years Day ( month=1, day=1, observance= ) - >>> July3rd = Holiday("July 3rd", month=7, day=3, days_of_week=(0, 1, 2, 3)) + >>> July3rd = pd.tseries.holiday.Holiday("July 3rd", month=7, day=3, days_of_week=(0, 1, 2, 3)) >>> July3rd Holiday: July 3rd (month=7, day=3, ) """ From 8a154c049418378086c8f05120d84fef27bf5088 Mon Sep 17 00:00:00 2001 From: Dushimirimana Salomon Date: Wed, 11 Aug 2021 03:02:48 -0500 Subject: [PATCH 2/5] Fix Style --- pandas/io/stata.py | 6 ++++-- pandas/tseries/holiday.py | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pandas/io/stata.py b/pandas/io/stata.py index b85b5092ae99b..3b8aba2738f63 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -3080,7 +3080,8 @@ class StataWriter117(StataWriter): Directly write a zip file >>> compression = {"method": "zip", "archive_name": "data_file.dta"} - >>> writer = pd.io.stata.StataWriter117('./data_file.zip', data, compression=compression) + >>> writer = pd.io.stata.StataWriter117('./data_file.zip', + ... data, compression=compression) >>> writer.write_file() Or with long strings stored in strl format @@ -3472,7 +3473,8 @@ class StataWriterUTF8(StataWriter117): Directly write a zip file >>> compression = {"method": "zip", "archive_name": "data_file.dta"} - >>> writer = pd.io.stata.StataWriterUTF8('./data_file.zip', data, compression=compression) + >>> writer = pd.io.stata.StataWriterUTF8('./data_file.zip', + ... data, compression=compression) >>> writer.write_file() Or with long strings stored in strl format diff --git a/pandas/tseries/holiday.py b/pandas/tseries/holiday.py index caa87a63625e6..c79ed62a8d8ff 100644 --- a/pandas/tseries/holiday.py +++ b/pandas/tseries/holiday.py @@ -197,14 +197,16 @@ class from pandas.tseries.offsets Holiday: July 3rd (month=7, day=3, ) >>> NewYears = pd.tseries.holiday.Holiday( - ... "New Years Day", month=1, day=1, observance=pd.tseries.holiday.nearest_workday + ... "New Years Day", month=1, day=1, + ... observance=pd.tseries.holiday.nearest_workday ... ) >>> NewYears # doctest: +SKIP Holiday: New Years Day ( month=1, day=1, observance= ) - >>> July3rd = pd.tseries.holiday.Holiday("July 3rd", month=7, day=3, days_of_week=(0, 1, 2, 3)) + >>> July3rd = pd.tseries.holiday.Holiday("July 3rd", month=7, + ... day=3, days_of_week=(0, 1, 2, 3)) >>> July3rd Holiday: July 3rd (month=7, day=3, ) """ From 81a6b827616cc9353da6bb45c89d4abad3e8d9a5 Mon Sep 17 00:00:00 2001 From: Dushimirimana Salomon Date: Wed, 11 Aug 2021 23:10:06 -0500 Subject: [PATCH 3/5] import statements standard convention --- pandas/io/formats/latex.py | 3 --- pandas/tseries/holiday.py | 14 +++++++------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/pandas/io/formats/latex.py b/pandas/io/formats/latex.py index 3ef843ac83009..3ffb60a042f69 100644 --- a/pandas/io/formats/latex.py +++ b/pandas/io/formats/latex.py @@ -488,7 +488,6 @@ def _select_iterator(self, over: str) -> type[RowStringIterator]: class LongTableBuilder(GenericTableBuilder): """Concrete table builder for longtable. - >>> import pandas as pd >>> from pandas.io.formats import format as fmt >>> df = pd.DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) >>> formatter = fmt.DataFrameFormatter(df) @@ -578,7 +577,6 @@ def env_end(self) -> str: class RegularTableBuilder(GenericTableBuilder): """Concrete table builder for regular table. - >>> import pandas as pd >>> from pandas.io.formats import format as fmt >>> df = pd.DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) >>> formatter = fmt.DataFrameFormatter(df) @@ -625,7 +623,6 @@ def env_end(self) -> str: class TabularBuilder(GenericTableBuilder): """Concrete table builder for tabular environment. - >>> import pandas as pd >>> from pandas.io.formats import format as fmt >>> df = pd.DataFrame({"a": [1, 2], "b": ["b1", "b2"]}) >>> formatter = fmt.DataFrameFormatter(df) diff --git a/pandas/tseries/holiday.py b/pandas/tseries/holiday.py index c79ed62a8d8ff..a12cdd8a78721 100644 --- a/pandas/tseries/holiday.py +++ b/pandas/tseries/holiday.py @@ -177,35 +177,35 @@ class from pandas.tseries.offsets Examples -------- - >>> import pandas as pd + >>> from pandas import tseries as ts >>> from dateutil.relativedelta import MO - >>> USMemorialDay = pd.tseries.holiday.Holiday( + >>> USMemorialDay = ts.holiday.Holiday( ... "Memorial Day", month=5, day=31, offset=pd.DateOffset(weekday=MO(-1)) ... ) >>> USMemorialDay Holiday: Memorial Day (month=5, day=31, offset=) - >>> USLaborDay = pd.tseries.holiday.Holiday( + >>> USLaborDay = ts.holiday.Holiday( ... "Labor Day", month=9, day=1, offset=pd.DateOffset(weekday=MO(1)) ... ) >>> USLaborDay Holiday: Labor Day (month=9, day=1, offset=) - >>> July3rd = pd.tseries.holiday.Holiday("July 3rd", month=7, day=3) + >>> July3rd = ts.holiday.Holiday("July 3rd", month=7, day=3) >>> July3rd Holiday: July 3rd (month=7, day=3, ) - >>> NewYears = pd.tseries.holiday.Holiday( + >>> NewYears = ts.holiday.Holiday( ... "New Years Day", month=1, day=1, - ... observance=pd.tseries.holiday.nearest_workday + ... observance=ts.holiday.nearest_workday ... ) >>> NewYears # doctest: +SKIP Holiday: New Years Day ( month=1, day=1, observance= ) - >>> July3rd = pd.tseries.holiday.Holiday("July 3rd", month=7, + >>> July3rd = ts.holiday.Holiday("July 3rd", month=7, ... day=3, days_of_week=(0, 1, 2, 3)) >>> July3rd Holiday: July 3rd (month=7, day=3, ) From 270ed4c5037d4c6957c9a33d659772b79ad6457f Mon Sep 17 00:00:00 2001 From: Dushimirimana Salomon Date: Thu, 12 Aug 2021 23:35:46 -0500 Subject: [PATCH 4/5] Corrected conventional import statements --- pandas/_testing/_io.py | 8 ++++---- pandas/_testing/asserters.py | 20 ++++++++++---------- pandas/core/algorithms.py | 8 ++++---- pandas/core/arrays/sparse/array.py | 4 ++-- pandas/core/dtypes/base.py | 6 +++--- pandas/core/dtypes/common.py | 8 ++++---- pandas/core/dtypes/concat.py | 20 ++++++++++---------- pandas/core/dtypes/inference.py | 12 ++++++------ pandas/core/generic.py | 8 ++++---- pandas/io/stata.py | 16 ++++++++-------- pandas/tseries/holiday.py | 15 +++++++-------- 11 files changed, 62 insertions(+), 63 deletions(-) diff --git a/pandas/_testing/_io.py b/pandas/_testing/_io.py index 0cdc8ac73ab52..a0b6963cfac97 100644 --- a/pandas/_testing/_io.py +++ b/pandas/_testing/_io.py @@ -160,8 +160,8 @@ def network( Tests decorated with @network will fail if it's possible to make a network connection to another URL (defaults to google.com):: - >>> import pandas as pd - >>> @pd._testing.network + >>> from pandas import _testing as ts + >>> @ts.network ... def test_network(): ... with pd.io.common.urlopen("rabbit://bonanza.com"): ... pass @@ -171,7 +171,7 @@ def network( You can specify alternative URLs:: - >>> @pd._testing.network("https://www.yahoo.com") + >>> @ts.network("https://www.yahoo.com") ... def test_something_with_yahoo(): ... raise IOError("Failure Message") >>> test_something_with_yahoo() @@ -182,7 +182,7 @@ def network( If you set check_before_test, it will check the url first and not run the test on failure:: - >>> @pd._testing.network("failing://url.blaher", check_before_test=True) + >>> @ts.network("failing://url.blaher", check_before_test=True) ... def test_something(): ... print("I ran!") ... raise ValueError("Failure") diff --git a/pandas/_testing/asserters.py b/pandas/_testing/asserters.py index 17da8c7a4d3db..4f308f48d24a3 100644 --- a/pandas/_testing/asserters.py +++ b/pandas/_testing/asserters.py @@ -303,10 +303,10 @@ def assert_index_equal( Examples -------- - >>> import pandas as pd + >>> from pandas import testing as tm >>> a = pd.Index([1, 2, 3]) >>> b = pd.Index([1, 2, 3]) - >>> pd.testing.assert_index_equal(a, b) + >>> tm.assert_index_equal(a, b) """ __tracebackhide__ = True @@ -794,10 +794,10 @@ def assert_extension_array_equal( Examples -------- - >>> import pandas as pd + >>> from pandas import testing as tm >>> a = pd.Series([1, 2, 3, 4]) >>> b, c = a.array, a.array - >>> pd.testing.assert_extension_array_equal(b, c) + >>> tm.assert_extension_array_equal(b, c) """ if check_less_precise is not no_default: warnings.warn( @@ -938,10 +938,10 @@ def assert_series_equal( Examples -------- - >>> import pandas as pd + >>> from pandas import testing as tm >>> a = pd.Series([1, 2, 3, 4]) >>> b = pd.Series([1, 2, 3, 4]) - >>> pd.testing.assert_series_equal(a, b) + >>> tm.assert_series_equal(a, b) """ __tracebackhide__ = True @@ -1203,17 +1203,17 @@ def assert_frame_equal( This example shows comparing two DataFrames that are equal but with columns of differing dtypes. - >>> import pandas as pd + >>> from pandas import _testing as ts >>> df1 = pd.DataFrame({'a': [1, 2], 'b': [3, 4]}) >>> df2 = pd.DataFrame({'a': [1, 2], 'b': [3.0, 4.0]}) df1 equals itself. - >>> pd._testing.assert_frame_equal(df1, df1) + >>> ts.assert_frame_equal(df1, df1) df1 differs from df2 as column 'b' is of a different type. - >>> pd._testing.assert_frame_equal(df1, df2) + >>> ts.assert_frame_equal(df1, df2) Traceback (most recent call last): ... AssertionError: Attributes of DataFrame.iloc[:, 1] (column name="b") are different @@ -1224,7 +1224,7 @@ def assert_frame_equal( Ignore differing dtypes in columns with check_dtype. - >>> pd._testing.assert_frame_equal(df1, df2, check_dtype=False) + >>> ts.assert_frame_equal(df1, df2, check_dtype=False) """ __tracebackhide__ = True diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 3421c5b5b01a6..845c824b745f3 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -1462,20 +1462,20 @@ def take( Examples -------- - >>> import pandas as pd + >>> from pandas.pai.extensions import take With the default ``allow_fill=False``, negative numbers indicate positional indices from the right. - >>> pd.api.extensions.take(np.array([10, 20, 30]), [0, 0, -1]) + >>> take(np.array([10, 20, 30]), [0, 0, -1]) array([10, 10, 30]) Setting ``allow_fill=True`` will place `fill_value` in those positions. - >>> pd.api.extensions.take(np.array([10, 20, 30]), [0, 0, -1], allow_fill=True) + >>> take(np.array([10, 20, 30]), [0, 0, -1], allow_fill=True) array([10., 10., nan]) - >>> pd.api.extensions.take(np.array([10, 20, 30]), [0, 0, -1], allow_fill=True, + >>> take(np.array([10, 20, 30]), [0, 0, -1], allow_fill=True, ... fill_value=-10) array([ 10, 10, -10]) """ diff --git a/pandas/core/arrays/sparse/array.py b/pandas/core/arrays/sparse/array.py index 6664880c897fc..b1cfcbd69a30b 100644 --- a/pandas/core/arrays/sparse/array.py +++ b/pandas/core/arrays/sparse/array.py @@ -282,8 +282,8 @@ class SparseArray(OpsMixin, PandasObject, ExtensionArray): Examples -------- - >>> import pandas as pd - >>> arr = pd.arrays.SparseArray([0, 0, 1, 2]) + >>> from pandas.arrays import SparseArray + >>> arr = SparseArray([0, 0, 1, 2]) >>> arr [0, 0, 1, 2] Fill: 0 diff --git a/pandas/core/dtypes/base.py b/pandas/core/dtypes/base.py index 0f727516c2043..372abee701b02 100644 --- a/pandas/core/dtypes/base.py +++ b/pandas/core/dtypes/base.py @@ -387,9 +387,9 @@ def register_extension_dtype(cls: type_t[ExtensionDtypeT]) -> type_t[ExtensionDt Examples -------- - >>> import pandas as pd - >>> @pd.api.extensions.register_extension_dtype - ... class MyExtensionDtype(pd.api.extensions.ExtensionDtype): + >>> from pandas.api.extensions import register_extension_dtype, ExtensionDtype + >>> @register_extension_dtype + ... class MyExtensionDtype(ExtensionDtype): ... name = "myextension" """ _registry.register(cls) diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index 579994170bb12..3a870c2287584 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -1450,15 +1450,15 @@ def is_extension_array_dtype(arr_or_dtype) -> bool: Examples -------- - >>> import pandas as pd + >>> from pandas.api.types import is_extension_array_dtype >>> arr = pd.Categorical(['a', 'b']) - >>> pd.api.types.is_extension_array_dtype(arr) + >>> is_extension_array_dtype(arr) True - >>> pd.api.types.is_extension_array_dtype(arr.dtype) + >>> is_extension_array_dtype(arr.dtype) True >>> arr = np.array(['a', 'b']) - >>> pd.api.types.is_extension_array_dtype(arr.dtype) + >>> is_extension_array_dtype(arr.dtype) False """ dtype = getattr(arr_or_dtype, "dtype", arr_or_dtype) diff --git a/pandas/core/dtypes/concat.py b/pandas/core/dtypes/concat.py index f5442a451ae9b..b0d00775bbed1 100644 --- a/pandas/core/dtypes/concat.py +++ b/pandas/core/dtypes/concat.py @@ -191,16 +191,16 @@ def union_categoricals( Examples -------- - >>> import pandas as pd + >>> from pandas.api.types import union_categoricals If you want to combine categoricals that do not necessarily have - the same categories, `pd.api.types.union_categoricals` will combine a list-like + the same categories, `union_categoricals` will combine a list-like of categoricals. The new categories will be the union of the categories being combined. >>> a = pd.Categorical(["b", "c"]) >>> b = pd.Categorical(["a", "b"]) - >>> pd.api.types.union_categoricals([a, b]) + >>> union_categoricals([a, b]) ['b', 'c', 'a', 'b'] Categories (3, object): ['b', 'c', 'a'] @@ -208,17 +208,17 @@ def union_categoricals( in the `categories` of the data. If you want the categories to be lexsorted, use `sort_categories=True` argument. - >>> pd.api.types.union_categoricals([a, b], sort_categories=True) + >>> union_categoricals([a, b], sort_categories=True) ['b', 'c', 'a', 'b'] Categories (3, object): ['a', 'b', 'c'] - `pd.api.types.union_categoricals` also works with the case of combining two + `union_categoricals` also works with the case of combining two categoricals of the same categories and order information (e.g. what you could also `append` for). >>> a = pd.Categorical(["a", "b"], ordered=True) >>> b = pd.Categorical(["a", "b", "a"], ordered=True) - >>> pd.api.types.union_categoricals([a, b]) + >>> union_categoricals([a, b]) ['a', 'b', 'a', 'b', 'a'] Categories (2, object): ['a' < 'b'] @@ -226,7 +226,7 @@ def union_categoricals( >>> a = pd.Categorical(["a", "b"], ordered=True) >>> b = pd.Categorical(["a", "b", "c"], ordered=True) - >>> pd.api.types.union_categoricals([a, b]) + >>> union_categoricals([a, b]) Traceback (most recent call last): ... TypeError: to union ordered Categoricals, all categories must be the same @@ -238,17 +238,17 @@ def union_categoricals( >>> a = pd.Categorical(["a", "b", "c"], ordered=True) >>> b = pd.Categorical(["c", "b", "a"], ordered=True) - >>> pd.api.types.union_categoricals([a, b], ignore_order=True) + >>> union_categoricals([a, b], ignore_order=True) ['a', 'b', 'c', 'c', 'b', 'a'] Categories (3, object): ['a', 'b', 'c'] - `pd.api.types.union_categoricals` also works with a `CategoricalIndex`, or `Series` + `union_categoricals` also works with a `CategoricalIndex`, or `Series` containing categorical data, but note that the resulting array will always be a plain `Categorical` >>> a = pd.Series(["b", "c"], dtype='category') >>> b = pd.Series(["a", "b"], dtype='category') - >>> pd.api.types.union_categoricals([a, b]) + >>> union_categoricals([a, b]) ['b', 'c', 'a', 'b'] Categories (3, object): ['b', 'c', 'a'] """ diff --git a/pandas/core/dtypes/inference.py b/pandas/core/dtypes/inference.py index 5b395aca8b919..1360b66e77dc0 100644 --- a/pandas/core/dtypes/inference.py +++ b/pandas/core/dtypes/inference.py @@ -51,20 +51,20 @@ def is_number(obj) -> bool: Examples -------- - >>> import pandas as pd - >>> pd.api.types.is_number(1) + >>> from pandas.api.types import is_number + >>> is_number(1) True - >>> pd.api.types.is_number(7.15) + >>> is_number(7.15) True Booleans are valid because they are int subclass. - >>> pd.api.types.is_number(False) + >>> is_number(False) True - >>> pd.api.types.is_number("foo") + >>> is_number("foo") False - >>> pd.api.types.is_number("5") + >>> is_number("5") False """ return isinstance(obj, (Number, np.number)) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 2e799557a1280..b2a4fecbf084d 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5681,8 +5681,8 @@ def astype( Convert to ordered categorical type with custom ordering: - >>> import pandas as pd - >>> cat_dtype = pd.api.types.CategoricalDtype( + >>> from pandas.api.types import CategoricalDtype + >>> cat_dtype = CategoricalDtype( ... categories=[2, 1], ordered=True) >>> ser.astype(cat_dtype) 0 1 @@ -8042,10 +8042,10 @@ def resample( To replace the use of the deprecated `loffset` argument: - >>> import pandas as pd + >>> from pandas.tseries.frequencies import to_offset >>> loffset = '19min' >>> ts_out = ts.resample('17min').sum() - >>> ts_out.index = ts_out.index + pd.tseries.frequencies.to_offset(loffset) + >>> ts_out.index = ts_out.index + to_offset(loffset) >>> ts_out 2000-10-01 23:33:00 0 2000-10-01 23:50:00 9 diff --git a/pandas/io/stata.py b/pandas/io/stata.py index 3b8aba2738f63..62fe692f464d3 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -3073,21 +3073,21 @@ class StataWriter117(StataWriter): Examples -------- - >>> import pandas as pd + >>> from pandas.io.stata import StataWriter117 >>> data = pd.DataFrame([[1.0, 1, 'a']], columns=['a', 'b', 'c']) - >>> writer = pd.io.stata.StataWriter117('./data_file.dta', data) + >>> writer = StataWriter117('./data_file.dta', data) >>> writer.write_file() Directly write a zip file >>> compression = {"method": "zip", "archive_name": "data_file.dta"} - >>> writer = pd.io.stata.StataWriter117('./data_file.zip', + >>> writer = StataWriter117('./data_file.zip', ... data, compression=compression) >>> writer.write_file() Or with long strings stored in strl format >>> data = pd.DataFrame([['A relatively long string'], [''], ['']], ... columns=['strls']) - >>> writer = pd.io.stata.StataWriter117('./data_file_with_long_strings.dta', data, + >>> writer = StataWriter117('./data_file_with_long_strings.dta', data, ... convert_strl=['strls']) >>> writer.write_file() """ @@ -3466,14 +3466,14 @@ class StataWriterUTF8(StataWriter117): -------- Using Unicode data and column names - >>> import pandas as pd + >>> from pandas.io.stata import StataWriterUTF8 >>> data = pd.DataFrame([[1.0, 1, 'ᴬ']], columns=['a', 'β', 'ĉ']) - >>> writer = pd.io.stata.StataWriterUTF8('./data_file.dta', data) + >>> writer = StataWriterUTF8('./data_file.dta', data) >>> writer.write_file() Directly write a zip file >>> compression = {"method": "zip", "archive_name": "data_file.dta"} - >>> writer = pd.io.stata.StataWriterUTF8('./data_file.zip', + >>> writer = StataWriterUTF8('./data_file.zip', ... data, compression=compression) >>> writer.write_file() @@ -3481,7 +3481,7 @@ class StataWriterUTF8(StataWriter117): >>> data = pd.DataFrame([['ᴀ relatively long ŝtring'], [''], ['']], ... columns=['strls']) - >>> writer = pd.io.stata.StataWriterUTF8('./data_file_with_long_strings.dta', data, + >>> writer = StataWriterUTF8('./data_file_with_long_strings.dta', data, ... convert_strl=['strls']) >>> writer.write_file() """ diff --git a/pandas/tseries/holiday.py b/pandas/tseries/holiday.py index a12cdd8a78721..15de48c416476 100644 --- a/pandas/tseries/holiday.py +++ b/pandas/tseries/holiday.py @@ -177,36 +177,35 @@ class from pandas.tseries.offsets Examples -------- - >>> from pandas import tseries as ts + >>> from pandas.tseries.holiday import Holiday, nearest_workday >>> from dateutil.relativedelta import MO - >>> USMemorialDay = ts.holiday.Holiday( + >>> USMemorialDay = Holiday( ... "Memorial Day", month=5, day=31, offset=pd.DateOffset(weekday=MO(-1)) ... ) >>> USMemorialDay Holiday: Memorial Day (month=5, day=31, offset=) - >>> USLaborDay = ts.holiday.Holiday( + >>> USLaborDay = Holiday( ... "Labor Day", month=9, day=1, offset=pd.DateOffset(weekday=MO(1)) ... ) >>> USLaborDay Holiday: Labor Day (month=9, day=1, offset=) - >>> July3rd = ts.holiday.Holiday("July 3rd", month=7, day=3) + >>> July3rd = Holiday("July 3rd", month=7, day=3) >>> July3rd Holiday: July 3rd (month=7, day=3, ) - >>> NewYears = ts.holiday.Holiday( + >>> NewYears = Holiday( ... "New Years Day", month=1, day=1, - ... observance=ts.holiday.nearest_workday + ... observance=nearest_workday ... ) >>> NewYears # doctest: +SKIP Holiday: New Years Day ( month=1, day=1, observance= ) - >>> July3rd = ts.holiday.Holiday("July 3rd", month=7, - ... day=3, days_of_week=(0, 1, 2, 3)) + >>> July3rd = Holiday("July 3rd", month=7, day=3, days_of_week=(0, 1, 2, 3)) >>> July3rd Holiday: July 3rd (month=7, day=3, ) """ From df25be207a9f506d7ac3807137bd2686e95f213b Mon Sep 17 00:00:00 2001 From: Dushimirimana Salomon Date: Mon, 16 Aug 2021 13:33:05 -0500 Subject: [PATCH 5/5] Update import statements and formating issues --- pandas/_testing/asserters.py | 8 ++++---- pandas/core/algorithms.py | 2 +- pandas/io/stata.py | 6 ++---- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/pandas/_testing/asserters.py b/pandas/_testing/asserters.py index 4f308f48d24a3..79c8e64fc4df3 100644 --- a/pandas/_testing/asserters.py +++ b/pandas/_testing/asserters.py @@ -1203,17 +1203,17 @@ def assert_frame_equal( This example shows comparing two DataFrames that are equal but with columns of differing dtypes. - >>> from pandas import _testing as ts + >>> from pandas._testing import assert_frame_equal >>> df1 = pd.DataFrame({'a': [1, 2], 'b': [3, 4]}) >>> df2 = pd.DataFrame({'a': [1, 2], 'b': [3.0, 4.0]}) df1 equals itself. - >>> ts.assert_frame_equal(df1, df1) + >>> assert_frame_equal(df1, df1) df1 differs from df2 as column 'b' is of a different type. - >>> ts.assert_frame_equal(df1, df2) + >>> assert_frame_equal(df1, df2) Traceback (most recent call last): ... AssertionError: Attributes of DataFrame.iloc[:, 1] (column name="b") are different @@ -1224,7 +1224,7 @@ def assert_frame_equal( Ignore differing dtypes in columns with check_dtype. - >>> ts.assert_frame_equal(df1, df2, check_dtype=False) + >>> assert_frame_equal(df1, df2, check_dtype=False) """ __tracebackhide__ = True diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 845c824b745f3..52c5790e1fc24 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -1462,7 +1462,7 @@ def take( Examples -------- - >>> from pandas.pai.extensions import take + >>> from pandas.api.extensions import take With the default ``allow_fill=False``, negative numbers indicate positional indices from the right. diff --git a/pandas/io/stata.py b/pandas/io/stata.py index 62fe692f464d3..1f87a017f31e4 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -3080,8 +3080,7 @@ class StataWriter117(StataWriter): Directly write a zip file >>> compression = {"method": "zip", "archive_name": "data_file.dta"} - >>> writer = StataWriter117('./data_file.zip', - ... data, compression=compression) + >>> writer = StataWriter117('./data_file.zip', data, compression=compression) >>> writer.write_file() Or with long strings stored in strl format @@ -3473,8 +3472,7 @@ class StataWriterUTF8(StataWriter117): Directly write a zip file >>> compression = {"method": "zip", "archive_name": "data_file.dta"} - >>> writer = StataWriterUTF8('./data_file.zip', - ... data, compression=compression) + >>> writer = StataWriterUTF8('./data_file.zip', data, compression=compression) >>> writer.write_file() Or with long strings stored in strl format