From 185ed0bc39bc0a8a92274b799cd3babf4ed3823b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Adri=C3=A1n=20Ca=C3=B1ones=20Castellano?= Date: Sat, 10 Mar 2018 23:49:18 +0100 Subject: [PATCH 1/7] DOC: update the docstring of pandas.DataFrame.from_dict --- pandas/core/frame.py | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index a66d00fff9714..376e91420574a 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -883,27 +883,51 @@ def dot(self, other): @classmethod def from_dict(cls, data, orient='columns', dtype=None, columns=None): """ - Construct DataFrame from dict of array-like or dicts + Construct DataFrame from dict of array-like or dicts. + + Creates DataFrame object from dictionary by columns or by index + allowing dtype specification. Parameters ---------- data : dict - {field : array-like} or {field : dict} + Of the form {field : array-like} or {field : dict}. orient : {'columns', 'index'}, default 'columns' The "orientation" of the data. If the keys of the passed dict should be the columns of the resulting DataFrame, pass 'columns' (default). Otherwise if the keys should be rows, pass 'index'. dtype : dtype, default None - Data type to force, otherwise infer - columns: list, default None + Data type to force, otherwise infer. + columns : list, default None Column labels to use when orient='index'. Raises a ValueError - if used with orient='columns' - + if used with orient='columns'. .. versionadded:: 0.23.0 + See Also + -------- + pandas.DataFrame.from_records : DataFrame from ndarray (structured dtype), + list of tuples, dict, or DataFrame + pandas.DataFrame: DataFrame object creation using constructor + Returns ------- - DataFrame + pandas.DataFrame + + Examples + -------- + >>> data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']} + >>> pd.DataFrame.from_dict(data) + col_1 col_2 + 0 3 a + 1 2 b + 2 1 c + 3 0 d + + >>> data = {'row_1': [3, 2, 1, 0], 'row_2': ['a', 'b', 'c', 'd']} + >>> pd.DataFrame.from_dict(data, orient='index') + 0 1 2 3 + row_1 3 2 1 0 + row_2 a b c d """ index = None orient = orient.lower() From a7b4191e7fbd8d0d428cac59cbad46686eb04f63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Adri=C3=A1n=20Ca=C3=B1ones=20Castellano?= Date: Sun, 11 Mar 2018 00:06:17 +0100 Subject: [PATCH 2/7] PEP-8 Fixing --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 376e91420574a..13772a51da5c3 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -906,7 +906,7 @@ def from_dict(cls, data, orient='columns', dtype=None, columns=None): See Also -------- pandas.DataFrame.from_records : DataFrame from ndarray (structured dtype), - list of tuples, dict, or DataFrame + list of tuples, dict, or DataFrame pandas.DataFrame: DataFrame object creation using constructor Returns From 978d57762aec9709233affcf4fe3a31d1e6bc0b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Adri=C3=A1n=20Ca=C3=B1ones=20Castellano?= Date: Sun, 11 Mar 2018 13:50:59 +0100 Subject: [PATCH 3/7] DOC: update the docstring of pandas.DataFrame.from_dict Added reviewers feedback --- pandas/core/frame.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 13772a51da5c3..4bb8af2ffade4 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -899,8 +899,8 @@ def from_dict(cls, data, orient='columns', dtype=None, columns=None): dtype : dtype, default None Data type to force, otherwise infer. columns : list, default None - Column labels to use when orient='index'. Raises a ValueError - if used with orient='columns'. + Column labels to use when ``orient='index'``. Raises a ValueError + if used with ``orient='columns'``. .. versionadded:: 0.23.0 See Also @@ -915,6 +915,8 @@ def from_dict(cls, data, orient='columns', dtype=None, columns=None): Examples -------- + By default the keys of the dict become the DataFrame columns: + >>> data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']} >>> pd.DataFrame.from_dict(data) col_1 col_2 @@ -923,6 +925,9 @@ def from_dict(cls, data, orient='columns', dtype=None, columns=None): 2 1 c 3 0 d + Specify ``orient='index'`` to create the DataFrame using dictionary + keys as rows: + >>> data = {'row_1': [3, 2, 1, 0], 'row_2': ['a', 'b', 'c', 'd']} >>> pd.DataFrame.from_dict(data, orient='index') 0 1 2 3 From 4f884a6eb1d12aa5b2688ae84a861aab91e3b9c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Adri=C3=A1n=20Ca=C3=B1ones=20Castellano?= Date: Sun, 11 Mar 2018 13:57:51 +0100 Subject: [PATCH 4/7] DOC: update the docstring of pandas.DataFrame.from_dict Fixed versionadded --- pandas/core/frame.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 4bb8af2ffade4..c7f720972986f 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -901,6 +901,7 @@ def from_dict(cls, data, orient='columns', dtype=None, columns=None): columns : list, default None Column labels to use when ``orient='index'``. Raises a ValueError if used with ``orient='columns'``. + .. versionadded:: 0.23.0 See Also From 9caaadddbf9f97603797f286fbc8be0d07e5e4e7 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Sun, 11 Mar 2018 15:35:50 +0100 Subject: [PATCH 5/7] small update --- pandas/core/frame.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index c7f720972986f..5065ac66dd926 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -904,16 +904,16 @@ def from_dict(cls, data, orient='columns', dtype=None, columns=None): .. versionadded:: 0.23.0 + Returns + ------- + pandas.DataFrame + See Also -------- pandas.DataFrame.from_records : DataFrame from ndarray (structured dtype), list of tuples, dict, or DataFrame pandas.DataFrame: DataFrame object creation using constructor - Returns - ------- - pandas.DataFrame - Examples -------- By default the keys of the dict become the DataFrame columns: @@ -934,6 +934,15 @@ def from_dict(cls, data, orient='columns', dtype=None, columns=None): 0 1 2 3 row_1 3 2 1 0 row_2 a b c d + + When using the 'index' orientation, the column names can be + specified manually: + + >>> pd.DataFrame.from_dict(data, orient='index', + ... columns=['A', 'B', 'C', 'D']) + A B C D + row_1 3 2 1 0 + row_2 a b c d """ index = None orient = orient.lower() From 29beaa1706078f842f008774b9f02b77ddb2214d Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Sun, 11 Mar 2018 15:37:17 +0100 Subject: [PATCH 6/7] pep8 --- pandas/core/frame.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 5065ac66dd926..97ad6a285ee89 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -901,7 +901,7 @@ def from_dict(cls, data, orient='columns', dtype=None, columns=None): columns : list, default None Column labels to use when ``orient='index'``. Raises a ValueError if used with ``orient='columns'``. - + .. versionadded:: 0.23.0 Returns @@ -910,8 +910,8 @@ def from_dict(cls, data, orient='columns', dtype=None, columns=None): See Also -------- - pandas.DataFrame.from_records : DataFrame from ndarray (structured dtype), - list of tuples, dict, or DataFrame + pandas.DataFrame.from_records : DataFrame from ndarray (structured + dtype), list of tuples, dict, or DataFrame pandas.DataFrame: DataFrame object creation using constructor Examples From b07d9fbd646d6c9298eb8c064cbe8dc0743ae394 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Sun, 11 Mar 2018 15:38:52 +0100 Subject: [PATCH 7/7] pep8 --- pandas/core/frame.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 97ad6a285ee89..d3a170a6260a3 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -910,9 +910,9 @@ def from_dict(cls, data, orient='columns', dtype=None, columns=None): See Also -------- - pandas.DataFrame.from_records : DataFrame from ndarray (structured + DataFrame.from_records : DataFrame from ndarray (structured dtype), list of tuples, dict, or DataFrame - pandas.DataFrame: DataFrame object creation using constructor + DataFrame : DataFrame object creation using constructor Examples -------- @@ -934,10 +934,10 @@ def from_dict(cls, data, orient='columns', dtype=None, columns=None): 0 1 2 3 row_1 3 2 1 0 row_2 a b c d - + When using the 'index' orientation, the column names can be specified manually: - + >>> pd.DataFrame.from_dict(data, orient='index', ... columns=['A', 'B', 'C', 'D']) A B C D