From e66ed485c203d2fefd1c5ecac1c4354381be4c1e Mon Sep 17 00:00:00 2001 From: Galuh Sahid Date: Wed, 15 Jan 2020 23:35:16 +0700 Subject: [PATCH 1/5] DOC: Add missing docstrings - pandas.Index.has_duplicates - pandas.Index.is_all_dates - pandas.Index.name - pandas.Index.is_boolean - pandas.Index.is_floating - pandas.Index.is_integer - pandas.Index.is_interval - pandas.Index.is_mixed - pandas.Index.is_numeric - pandas.Index.is_object --- pandas/core/indexes/base.py | 204 ++++++++++++++++++++++++++++++++++++ 1 file changed, 204 insertions(+) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 47daaa4958411..a2373aeb60518 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1644,21 +1644,184 @@ def is_unique(self) -> bool: @property def has_duplicates(self) -> bool: + """ + Check if the Index has duplicate values. + + Returns + ------- + boolean + Whether or not the Index has duplicate values. + + Examples + -------- + >>> idx = pd.Index([1, 5, 7, 7]) + >>> idx.has_duplicates + True + + >>> idx = pd.Index([1, 5, 7]) + >>> idx.has_duplicates + False + + >>> idx = pd.Index(["Watermelon", "Orange", "Apple", + ... "Watermelon"]).astype("category") + >>> idx.has_duplicates + True + + >>> idx = pd.Index(["Orange", "Apple", + ... "Watermelon"]).astype("category") + >>> idx.has_duplicates + False + """ return not self.is_unique def is_boolean(self) -> bool: + """ + Check if the Index only consists of booleans. + + Returns + ------- + boolean + Whether or not the Index only consists of booleans. + + Examples + -------- + >>> idx = pd.Index([True, False, True]) + >>> idx.is_boolean() + True + + >>> idx = pd.Index(["True", "False", "True"]) + >>> idx.is_boolean() + False + + >>> idx = pd.Index([True, False, "True"]) + >>> idx.is_boolean() + False + """ return self.inferred_type in ["boolean"] def is_integer(self) -> bool: + """ + Check if the Index only consists of integers. + + Returns + ------- + boolean + Whether or not the Index only consists of integers. + + Examples + -------- + >>> idx = pd.Index([1, 2, 3, 4]) + >>> idx.is_integer() + True + + >>> idx = pd.Index([1.0, 2.0, 3.0, 4.0]) + >>> idx.is_integer() + False + + >>> idx = pd.Index([1, 2, 3, 4.0]) + >>> idx.is_integer() + False + """ return self.inferred_type in ["integer"] def is_floating(self) -> bool: + """ + Check if the Index only consists of floats, NaNs, or + a mix of floats, integers, or NaNs. + + Returns + ------- + boolean + Whether or not the Index only consists of only consists of floats, NaNs, or + a mix of floats, integers, or NaNs. + + Examples + -------- + >>> idx = pd.Index([1.0, 2.0, 3.0, 4.0]) + >>> idx.is_floating() + True + + >>> idx = pd.Index([1, 2, 3, 4.0]) + >>> idx.is_floating() + True + + >>> idx = pd.Index([1, 2, 3, 4.0, np.nan]) + >>> idx.is_floating() + True + + >>> idx = pd.Index([1, 2, 3, 4, np.nan]) + >>> idx.is_floating() + True + + >>> idx = pd.Index([1, 2, 3, 4]) + >>> idx.is_integer() + False + """ return self.inferred_type in ["floating", "mixed-integer-float", "integer-na"] def is_numeric(self) -> bool: + """ + Check if the Index only consists of numeric + data. + + Returns + ------- + boolean + Whether or not the Index only only consists of numeric + data. + + Examples + -------- + >>> idx = pd.Index([1.0, 2.0, 3.0, 4.0]) + >>> idx.is_numeric() + True + + >>> idx = pd.Index([1, 2, 3, 4.0]) + >>> idx.is_numeric() + True + + >>> idx = pd.Index([1, 2, 3, 4]) + >>> idx.is_numeric() + True + + >>> idx = pd.Index([1, 2, 3, 4.0, np.nan]) + >>> idx.is_numeric() + True + + >>> idx = pd.Index([1, 2, 3, 4.0, np.nan, "Apple"]) + >>> idx.is_numeric() + False + """ return self.inferred_type in ["integer", "floating"] def is_object(self) -> bool: + """ + Check if the Index is of the object dtype. + + Returns + ------- + boolean + Whether or not the Index is of the object dtype. + + Examples + -------- + >>> idx = pd.Index(["Apple", "Mango", "Watermelon"]) + >>> idx.is_object() + True + + >>> idx = pd.Index(["Apple", "Mango", 2.0]) + >>> idx.is_object() + True + + >>> idx = pd.Index(["Watermelon", "Orange", "Apple", + ... "Watermelon"]).astype("category") + >>> idx.object() + False + + >>> idx = pd.Index([1.0, 2.0, 3.0, 4.0]) + >>> idx.is_object() + False + """ return is_object_dtype(self.dtype) def is_categorical(self) -> bool: @@ -1698,9 +1861,50 @@ def is_categorical(self) -> bool: return self.inferred_type in ["categorical"] def is_interval(self) -> bool: + """ + Check if the Index holds Interval objects. + + Returns + ------- + boolean + Whether or not the Index holds Interval objects. + + See Also + -------- + IntervalIndex : Index for Interval objects. + + Examples + -------- + >>> idx = pd.Index([pd.Interval(left=0, right=5), + ... pd.Interval(left=5, right=10)]) + >>> idx.is_interval() + True + + >>> idx = pd.Index([1, 3, 5, 7]) + >>> idx.is_interval() + False + """ return self.inferred_type in ["interval"] def is_mixed(self) -> bool: + """ + Check if the Index holds data with mixed data types. + + Returns + ------- + boolean + Whether or not the Index holds data with mixed data types. + + Examples + -------- + >>> idx = pd.Index(['a', np.nan, 'b']) + >>> idx.is_mixed() + True + + >>> idx = pd.Index([1.0, 2.0, 3.0, 5.0]) + >>> idx.is_mixed() + False + """ return self.inferred_type in ["mixed"] def holds_integer(self): From 69f4ec09f7b379b2ef53a2c41ea2065bec8d2cdc Mon Sep 17 00:00:00 2001 From: Galuh Sahid Date: Wed, 15 Jan 2020 23:44:58 +0700 Subject: [PATCH 2/5] DOC: Add is_all_dates and name --- pandas/core/indexes/base.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index a2373aeb60518..364f542de062b 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1163,6 +1163,9 @@ def to_frame(self, index=True, name=None): @property def name(self): + """ + Return Index or MultiIndex name. + """ return self._name @name.setter @@ -1922,6 +1925,9 @@ def inferred_type(self): @cache_readonly def is_all_dates(self) -> bool: + """ + Whether or not the index values only consist of dates. + """ return is_datetime_array(ensure_object(self.values)) # -------------------------------------------------------------------- From 3b796fd12a0e53e24fa515cb2fa0456a7fc5a365 Mon Sep 17 00:00:00 2001 From: Galuh Sahid Date: Thu, 16 Jan 2020 00:20:45 +0700 Subject: [PATCH 3/5] DOC: Fix bools, add See More --- pandas/core/indexes/base.py | 101 +++++++++++++++++++++++++++++++----- 1 file changed, 88 insertions(+), 13 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 364f542de062b..ce234d965df6b 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1652,7 +1652,7 @@ def has_duplicates(self) -> bool: Returns ------- - boolean + bool Whether or not the Index has duplicate values. Examples @@ -1683,9 +1683,19 @@ def is_boolean(self) -> bool: Returns ------- - boolean + bool Whether or not the Index only consists of booleans. + See Also + -------- + is_integer : Check if the Index only consists of integers. + is_floating : Check if the Index is a floating type. + is_numeric : Check if the Index only consists of numeric data. + is_object : Check if the Index is of the object dtype. + is_categorical : Check if the Index holds categorical data. + is_interval : Check if the Index holds Interval objects. + is_mixed : Check if the Index holds data with mixed data types. + Examples -------- >>> idx = pd.Index([True, False, True]) @@ -1706,9 +1716,19 @@ def is_integer(self) -> bool: """ Check if the Index only consists of integers. + See Also + -------- + is_boolean : Check if the Index only consists of booleans. + is_floating : Check if the Index is a floating type. + is_numeric : Check if the Index only consists of numeric data. + is_object : Check if the Index is of the object dtype. + is_categorical : Check if the Index holds categorical data. + is_interval : Check if the Index holds Interval objects. + is_mixed : Check if the Index holds data with mixed data types. + Returns ------- - boolean + bool Whether or not the Index only consists of integers. Examples @@ -1729,15 +1749,27 @@ def is_integer(self) -> bool: def is_floating(self) -> bool: """ - Check if the Index only consists of floats, NaNs, or - a mix of floats, integers, or NaNs. + Check if the Index is a floating type. + + The Index may consist of only floats, NaNs, or a mix of floats, + integers, or NaNs. Returns ------- - boolean + bool Whether or not the Index only consists of only consists of floats, NaNs, or a mix of floats, integers, or NaNs. + See Also + -------- + is_boolean : Check if the Index only consists of booleans. + is_integer : Check if the Index only consists of integers. + is_numeric : Check if the Index only consists of numeric data. + is_object : Check if the Index is of the object dtype. + is_categorical : Check if the Index holds categorical data. + is_interval : Check if the Index holds Interval objects. + is_mixed : Check if the Index holds data with mixed data types. + Examples -------- >>> idx = pd.Index([1.0, 2.0, 3.0, 4.0]) @@ -1764,15 +1796,24 @@ def is_floating(self) -> bool: def is_numeric(self) -> bool: """ - Check if the Index only consists of numeric - data. + Check if the Index only consists of numeric data. Returns ------- - boolean + bool Whether or not the Index only only consists of numeric data. + See Also + -------- + is_boolean : Check if the Index only consists of booleans. + is_integer : Check if the Index only consists of integers. + is_floating : Check if the Index is a floating type. + is_object : Check if the Index is of the object dtype. + is_categorical : Check if the Index holds categorical data. + is_interval : Check if the Index holds Interval objects. + is_mixed : Check if the Index holds data with mixed data types. + Examples -------- >>> idx = pd.Index([1.0, 2.0, 3.0, 4.0]) @@ -1803,9 +1844,19 @@ def is_object(self) -> bool: Returns ------- - boolean + bool Whether or not the Index is of the object dtype. + See Also + -------- + is_boolean : Check if the Index only consists of booleans. + is_integer : Check if the Index only consists of integers. + is_floating : Check if the Index is a floating type. + is_numeric : Check if the Index only consists of numeric data. + is_categorical : Check if the Index holds categorical data. + is_interval : Check if the Index holds Interval objects. + is_mixed : Check if the Index holds data with mixed data types. + Examples -------- >>> idx = pd.Index(["Apple", "Mango", "Watermelon"]) @@ -1833,12 +1884,19 @@ def is_categorical(self) -> bool: Returns ------- - boolean + bool True if the Index is categorical. See Also -------- CategoricalIndex : Index for categorical data. + is_boolean : Check if the Index only consists of booleans. + is_integer : Check if the Index only consists of integers. + is_floating : Check if the Index is a floating type. + is_numeric : Check if the Index only consists of numeric data. + is_object : Check if the Index is of the object dtype. + is_interval : Check if the Index holds Interval objects. + is_mixed : Check if the Index holds data with mixed data types. Examples -------- @@ -1869,12 +1927,19 @@ def is_interval(self) -> bool: Returns ------- - boolean + bool Whether or not the Index holds Interval objects. See Also -------- IntervalIndex : Index for Interval objects. + is_boolean : Check if the Index only consists of booleans. + is_integer : Check if the Index only consists of integers. + is_floating : Check if the Index is a floating type. + is_numeric : Check if the Index only consists of numeric data. + is_object : Check if the Index is of the object dtype. + is_categorical : Check if the Index holds categorical data. + is_mixed : Check if the Index holds data with mixed data types. Examples -------- @@ -1895,9 +1960,19 @@ def is_mixed(self) -> bool: Returns ------- - boolean + bool Whether or not the Index holds data with mixed data types. + See Also + -------- + is_boolean : Check if the Index only consists of booleans. + is_integer : Check if the Index only consists of integers. + is_floating : Check if the Index is a floating type. + is_numeric : Check if the Index only consists of numeric data. + is_object : Check if the Index is of the object dtype. + is_categorical : Check if the Index holds categorical data. + is_interval : Check if the Index holds Interval objects. + Examples -------- >>> idx = pd.Index(['a', np.nan, 'b']) From dcab448a70dc9fb1b361188704b92f61b1b6af03 Mon Sep 17 00:00:00 2001 From: Galuh Sahid Date: Fri, 17 Jan 2020 00:51:58 +0700 Subject: [PATCH 4/5] DOC: Remove typos and some examples --- pandas/core/indexes/base.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index ce234d965df6b..98f3fe93e48be 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1716,6 +1716,11 @@ def is_integer(self) -> bool: """ Check if the Index only consists of integers. + Returns + ------- + bool + Whether or not the Index only consists of integers. + See Also -------- is_boolean : Check if the Index only consists of booleans. @@ -1726,11 +1731,6 @@ def is_integer(self) -> bool: is_interval : Check if the Index holds Interval objects. is_mixed : Check if the Index holds data with mixed data types. - Returns - ------- - bool - Whether or not the Index only consists of integers. - Examples -------- >>> idx = pd.Index([1, 2, 3, 4]) @@ -1741,7 +1741,7 @@ def is_integer(self) -> bool: >>> idx.is_integer() False - >>> idx = pd.Index([1, 2, 3, 4.0]) + >>> idx = pd.Index(["Apple", "Mango", "Watermelon"]) >>> idx.is_integer() False """ @@ -1776,11 +1776,7 @@ def is_floating(self) -> bool: >>> idx.is_floating() True - >>> idx = pd.Index([1, 2, 3, 4.0]) - >>> idx.is_floating() - True - - >>> idx = pd.Index([1, 2, 3, 4.0, np.nan]) + >>> idx = pd.Index([1.0, 2.0, np.nan, 4.0]) >>> idx.is_floating() True @@ -1801,8 +1797,7 @@ def is_numeric(self) -> bool: Returns ------- bool - Whether or not the Index only only consists of numeric - data. + Whether or not the Index only consists of numeric data. See Also -------- From 755a0f77f25ab990047f4dd9d97e9a6220bfc7dd Mon Sep 17 00:00:00 2001 From: Galuh Sahid Date: Fri, 17 Jan 2020 09:41:13 +0700 Subject: [PATCH 5/5] DOC: Fix typo --- pandas/core/indexes/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 98f3fe93e48be..605013882b31e 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -1785,7 +1785,7 @@ def is_floating(self) -> bool: True >>> idx = pd.Index([1, 2, 3, 4]) - >>> idx.is_integer() + >>> idx.is_floating() False """ return self.inferred_type in ["floating", "mixed-integer-float", "integer-na"]