From 60e3dc20515f5f5a1fb6efb3c7ce840f9e704a20 Mon Sep 17 00:00:00 2001 From: coffeedjason Date: Sun, 11 Mar 2018 17:47:16 +0900 Subject: [PATCH 1/2] revise docstring for several functions and properties --- pandas/core/frame.py | 36 +++++++++++----------- pandas/core/generic.py | 69 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 84 insertions(+), 21 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index a66d00fff9714..4db3b6b0638bb 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2432,8 +2432,8 @@ def eval(self, expr, inplace=False, **kwargs): return _eval(expr, inplace=inplace, **kwargs) def select_dtypes(self, include=None, exclude=None): - """Return a subset of a DataFrame including/excluding columns based on - their ``dtype``. + """ + Return a subset of the DataFrame based on the column dtypes. Parameters ---------- @@ -2471,19 +2471,19 @@ def select_dtypes(self, include=None, exclude=None): Examples -------- - >>> df = pd.DataFrame({'a': np.random.randn(6).astype('f4'), + >>> df = pd.DataFrame({'a': [1, 2] * 3, ... 'b': [True, False] * 3, ... 'c': [1.0, 2.0] * 3}) >>> df a b c - 0 0.3962 True 1.0 - 1 0.1459 False 2.0 - 2 0.2623 True 1.0 - 3 0.0764 False 2.0 - 4 -0.9703 True 1.0 - 5 -1.2094 False 2.0 + 0 1 True 1.0 + 1 2 False 2.0 + 2 1 True 1.0 + 3 2 False 2.0 + 4 1 True 1.0 + 5 2 False 2.0 >>> df.select_dtypes(include='bool') - c + b 0 True 1 False 2 True @@ -2498,14 +2498,14 @@ def select_dtypes(self, include=None, exclude=None): 3 2.0 4 1.0 5 2.0 - >>> df.select_dtypes(exclude=['floating']) - b - 0 True - 1 False - 2 True - 3 False - 4 True - 5 False + >>> df.select_dtypes(exclude=['int']) + b c + 0 True 1.0 + 1 False 2.0 + 2 True 1.0 + 3 False 2.0 + 4 True 1.0 + 5 False 2.0 """ if not is_list_like(include): diff --git a/pandas/core/generic.py b/pandas/core/generic.py index a893b2ba1a189..14cbcac42f871 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -4260,16 +4260,79 @@ def _get_values(self): return self.values def get_values(self): - """same as values (but handles sparseness conversions)""" + """ + Return a NumPy representation of the data + after converting sparse to dense. + + Returns + ------- + numpy.ndarray + Numpy representation of DataFrame + + See Also + -------- + values : Numpy representation of DataFrame + + Examples + -------- + >>> df = pd.DataFrame({'a': [1, 2], 'b': [True, False], + ... 'c': [1.0, 2.0]}) + >>> df.get_values() + array([[1, True, 1.0], [2, False, 2.0]], dtype=object) + """ return self.values def get_dtype_counts(self): - """Return the counts of dtypes in this object.""" + """ + Return counts of unique dtypes in this object. + + Returns + ------- + dtype : Series + Series with the count of columns with each dtype + + See Also + -------- + dtypes : Return the dtypes in this object. + + Examples + -------- + >>> a = [['a', 1, 1.0], ['b', 2, 2.0], ['c', 3, 3.0]] + >>> df = pd.DataFrame(a, columns=['str', 'int', 'float']) + >>> df.get_dtype_counts() + float64 1 + int64 1 + object 1 + dtype: int64 + """ from pandas import Series return Series(self._data.get_dtype_counts()) def get_ftype_counts(self): - """Return the counts of ftypes in this object.""" + """ + Return counts of unique ftypes in this object. + + Returns + ------- + dtype : Series + Series with the count of columns + with each type and sparcity(dense/sparse) + + See Also + -------- + ftypes : Return + ftypes (indication of sparse/dense and dtype) in this object. + + Examples + -------- + >>> a = [['a', 1, 1.0], ['b', 2, 2.0], ['c', 3, 3.0]] + >>> df = pd.DataFrame(a, columns=['str', 'int', 'float']) + >>> df.get_ftype_counts() + float64:dense 1 + int64:dense 1 + object:dense 1 + dtype: int64 + """ from pandas import Series return Series(self._data.get_ftype_counts()) From 61851b288cc4209a60e06cb7890a4930151cf601 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Tue, 13 Mar 2018 09:43:08 -0500 Subject: [PATCH 2/2] Updats --- pandas/core/frame.py | 5 +++- pandas/core/generic.py | 53 +++++++++++++++++++++++++++++++++++------- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 4db3b6b0638bb..7092887975727 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2433,7 +2433,7 @@ def eval(self, expr, inplace=False, **kwargs): def select_dtypes(self, include=None, exclude=None): """ - Return a subset of the DataFrame based on the column dtypes. + Return a subset of the DataFrame's columns based on the column dtypes. Parameters ---------- @@ -2482,6 +2482,7 @@ def select_dtypes(self, include=None, exclude=None): 3 2 False 2.0 4 1 True 1.0 5 2 False 2.0 + >>> df.select_dtypes(include='bool') b 0 True @@ -2490,6 +2491,7 @@ def select_dtypes(self, include=None, exclude=None): 3 False 4 True 5 False + >>> df.select_dtypes(include=['float64']) c 0 1.0 @@ -2498,6 +2500,7 @@ def select_dtypes(self, include=None, exclude=None): 3 2.0 4 1.0 5 2.0 + >>> df.select_dtypes(exclude=['int']) b c 0 True 1.0 diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 14cbcac42f871..0074665505fee 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -4261,8 +4261,11 @@ def _get_values(self): def get_values(self): """ - Return a NumPy representation of the data - after converting sparse to dense. + Return an ndarray after converting sparse values to dense. + + This is the same as ``.values`` for non-sparse data. For sparse + data contained in a `pandas.SparseArray`, the data are first + converted to a dense representation. Returns ------- @@ -4271,14 +4274,33 @@ def get_values(self): See Also -------- - values : Numpy representation of DataFrame + values : Numpy representation of DataFrame. + pandas.SparseArray : Container for sparse data. Examples -------- >>> df = pd.DataFrame({'a': [1, 2], 'b': [True, False], ... 'c': [1.0, 2.0]}) + >>> df + a b c + 0 1 True 1.0 + 1 2 False 2.0 + >>> df.get_values() array([[1, True, 1.0], [2, False, 2.0]], dtype=object) + + >>> df = pd.DataFrame({"a": pd.SparseArray([1, None, None]), + ... "c": [1.0, 2.0, 3.0]}) + >>> df + a c + 0 1.0 1.0 + 1 NaN 2.0 + 2 NaN 3.0 + + >>> df.get_values() + array([[ 1., 1.], + [nan, 2.], + [nan, 3.]]) """ return self.values @@ -4289,7 +4311,7 @@ def get_dtype_counts(self): Returns ------- dtype : Series - Series with the count of columns with each dtype + Series with the count of columns with each dtype. See Also -------- @@ -4299,6 +4321,12 @@ def get_dtype_counts(self): -------- >>> a = [['a', 1, 1.0], ['b', 2, 2.0], ['c', 3, 3.0]] >>> df = pd.DataFrame(a, columns=['str', 'int', 'float']) + >>> df + str int float + 0 a 1 1.0 + 1 b 2 2.0 + 2 c 3 3.0 + >>> df.get_dtype_counts() float64 1 int64 1 @@ -4312,21 +4340,30 @@ def get_ftype_counts(self): """ Return counts of unique ftypes in this object. + This is useful for SparseDataFrame or for DataFrames containing + sparse arrays. + Returns ------- dtype : Series - Series with the count of columns - with each type and sparcity(dense/sparse) + Series with the count of columns with each type and + sparsity (dense/sparse) See Also -------- - ftypes : Return - ftypes (indication of sparse/dense and dtype) in this object. + ftypes : Return ftypes (indication of sparse/dense and dtype) in + this object. Examples -------- >>> a = [['a', 1, 1.0], ['b', 2, 2.0], ['c', 3, 3.0]] >>> df = pd.DataFrame(a, columns=['str', 'int', 'float']) + >>> df + str int float + 0 a 1 1.0 + 1 b 2 2.0 + 2 c 3 3.0 + >>> df.get_ftype_counts() float64:dense 1 int64:dense 1