From ccbbad683f113355f7a679b785403e737d9fd6e0 Mon Sep 17 00:00:00 2001 From: VincentLa Date: Wed, 24 May 2017 14:56:30 -0700 Subject: [PATCH 1/4] Adding some more documentation on dataframe with regards to dtype --- pandas/core/frame.py | 45 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 78a369761afc1..02404d7235031 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -241,17 +241,52 @@ class DataFrame(NDFrame): Column labels to use for resulting frame. Will default to np.arange(n) if no column labels are provided dtype : dtype, default None - Data type to force, otherwise infer + Data type to force. Only a single dtype is allowed. If None, infer copy : boolean, default False Copy data from inputs. Only affects DataFrame / 2d ndarray input Examples -------- - >>> d = {'col1': ts1, 'col2': ts2} - >>> df = DataFrame(data=d, index=index) - >>> df2 = DataFrame(np.random.randn(10, 5)) - >>> df3 = DataFrame(np.random.randn(10, 5), + Constructing DataFrame from a dictionary. + + >>> d = {'col1': [1, 2], 'col2': [1, 2]} + >>> df = DataFrame(data=d) + >>> df + col1 col2 + 0 1 3 + 1 2 4 + + Notice that the inferred dtype is int64. + + >>> df.dtypes + col1 int64 + col2 int64 + dtype: object + + To enforce a single dtype: + + >>> df = DataFrame(data=d, dtype=np.int8) + >>> df.dtypes + col1 int8 + col2 int8 + dtype: object + + Constructing DataFrame from numpy ndarray: + + >>> df2 = DataFrame(np.random.randn(10, 5), ... columns=['a', 'b', 'c', 'd', 'e']) + >>> df2 + a b c d e + 0 -0.783492 0.563675 -0.864924 0.391128 0.353971 + 1 1.254319 0.375264 0.592395 -0.573814 0.093112 + 2 -0.548918 0.334120 0.263741 0.007451 0.536851 + 3 -1.266109 0.472464 0.605991 0.183181 1.605140 + 4 -0.136033 -1.540123 -0.625398 -1.113922 -0.314596 + 5 0.497200 -0.793684 -1.077530 0.015950 1.194512 + 6 0.074891 -0.356493 1.094723 -0.695969 -0.318360 + 7 0.507182 -1.311427 -0.479544 -0.825982 -1.013853 + 8 -0.379124 -1.314563 -0.824940 0.587772 1.334155 + 9 0.389357 -1.399791 -1.498242 -0.312420 0.533206 See also -------- From 7f6b7f49ff3e3f99fe32a7ab4d92d20064cc0fd2 Mon Sep 17 00:00:00 2001 From: VincentLa Date: Wed, 24 May 2017 16:27:46 -0700 Subject: [PATCH 2/4] Making example for creating dataframe from np matrix easier --- pandas/core/frame.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 02404d7235031..4e44c10741422 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -273,20 +273,15 @@ class DataFrame(NDFrame): Constructing DataFrame from numpy ndarray: - >>> df2 = DataFrame(np.random.randn(10, 5), - ... columns=['a', 'b', 'c', 'd', 'e']) + >>> df2 = pd.DataFrame(np.random.randint(low=0, high=10, size=(5, 5)), + ... columns=['a', 'b', 'c', 'd', 'e']) >>> df2 - a b c d e - 0 -0.783492 0.563675 -0.864924 0.391128 0.353971 - 1 1.254319 0.375264 0.592395 -0.573814 0.093112 - 2 -0.548918 0.334120 0.263741 0.007451 0.536851 - 3 -1.266109 0.472464 0.605991 0.183181 1.605140 - 4 -0.136033 -1.540123 -0.625398 -1.113922 -0.314596 - 5 0.497200 -0.793684 -1.077530 0.015950 1.194512 - 6 0.074891 -0.356493 1.094723 -0.695969 -0.318360 - 7 0.507182 -1.311427 -0.479544 -0.825982 -1.013853 - 8 -0.379124 -1.314563 -0.824940 0.587772 1.334155 - 9 0.389357 -1.399791 -1.498242 -0.312420 0.533206 + a b c d e + 0 2 8 8 3 4 + 1 4 2 9 0 9 + 2 1 0 7 8 0 + 3 5 1 7 1 3 + 4 6 0 2 4 2 See also -------- From 6108d69f5b3baf634a6ac58384f777a91526e80a Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Wed, 31 May 2017 10:48:11 +0200 Subject: [PATCH 3/4] small edit --- pandas/core/frame.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index a3e94d4cb9da5..49250d9d19366 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -250,11 +250,11 @@ class DataFrame(NDFrame): Constructing DataFrame from a dictionary. >>> d = {'col1': [1, 2], 'col2': [1, 2]} - >>> df = DataFrame(data=d) + >>> df = pd.DataFrame(data=d) >>> df - col1 col2 - 0 1 3 - 1 2 4 + col1 col2 + 0 1 1 + 1 2 2 Notice that the inferred dtype is int64. @@ -265,7 +265,7 @@ class DataFrame(NDFrame): To enforce a single dtype: - >>> df = DataFrame(data=d, dtype=np.int8) + >>> df = pd.DataFrame(data=d, dtype=np.int8) >>> df.dtypes col1 int8 col2 int8 From edc1c046c596d086c91cf7af2d62c2adc75e3258 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Wed, 31 May 2017 10:51:49 +0200 Subject: [PATCH 4/4] another edit --- 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 49250d9d19366..907959c42323e 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -249,12 +249,12 @@ class DataFrame(NDFrame): -------- Constructing DataFrame from a dictionary. - >>> d = {'col1': [1, 2], 'col2': [1, 2]} + >>> d = {'col1': [1, 2], 'col2': [3, 4]} >>> df = pd.DataFrame(data=d) >>> df col1 col2 - 0 1 1 - 1 2 2 + 0 1 3 + 1 2 4 Notice that the inferred dtype is int64.