From 860f474e66f39edf06b8d11549791adb1b5b7906 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Sun, 14 Oct 2018 23:31:09 +0100 Subject: [PATCH 1/7] Updated examples --- pandas/core/frame.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 8d6e403783fc9..ce344fb4dcacb 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1856,12 +1856,15 @@ def to_stata(self, fname, convert_dates=None, write_index=True, data_label=None, variable_labels=None, version=114, convert_strl=None): """ - Export Stata binary dta files. + Converting data frame object to Stata dta format. + + Writes the Dataframe to a Stata dataset file. + "dta" files contain a Stata dataset. Parameters ---------- fname : path (string), buffer or path object - string, path object (pathlib.Path or py._path.local.LocalPath) or + String, path object (pathlib.Path or py._path.local.LocalPath) or object implementing a binary write() functions. If using a buffer then the buffer will not be automatically closed after the file data has been written. @@ -1921,27 +1924,27 @@ def to_stata(self, fname, convert_dates=None, write_index=True, See Also -------- - pandas.read_stata : Import Stata data files. - pandas.io.stata.StataWriter : Low-level writer for Stata data files. - pandas.io.stata.StataWriter117 : Low-level writer for version 117 - files. + read_stata : Import Stata data files. + io.stata.StataWriter : Low-level writer for Stata data files. + io.stata.StataWriter117 : Low-level writer for version 117 files. Examples -------- - >>> data.to_stata('./data_file.dta') - - Or with dates + Converting dataframe with date column to Stata dta file + using the to_stata method. - >>> data.to_stata('./date_data_file.dta', {2 : 'tw'}) + >>> dates = pd.date_range(start='2018-01-01', periods=4) + >>> df = pd.DataFrame({'date': dates, + ... 'animal': ['falcon', 'parrot', 'falcon', + ... 'parrot'], + ... 'speed': [350, 18, 361, 15]}).set_index(['date', + ... 'animal']) + >>> df.to_stata('animals.dta') Alternatively you can create an instance of the StataWriter class - >>> writer = StataWriter('./data_file.dta', data) - >>> writer.write_file() - - With dates: - - >>> writer = StataWriter('./date_data_file.dta', data, {2 : 'tw'}) + >>> StataWriter = pd.io.stata.StataWriter + >>> writer = StataWriter('animals.dta', df) >>> writer.write_file() """ kwargs = {} From 1b7bac435f557d14203e7dc00ee6cbc92cab75a5 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Mon, 15 Oct 2018 00:13:47 +0100 Subject: [PATCH 2/7] Add returns section --- pandas/core/frame.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index ce344fb4dcacb..275511961f779 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1909,6 +1909,10 @@ def to_stata(self, fname, convert_dates=None, write_index=True, .. versionadded:: 0.23.0 + Returns + ------ + Stata (dta) file or StataWriter. + Raises ------ NotImplementedError @@ -1921,7 +1925,7 @@ def to_stata(self, fname, convert_dates=None, write_index=True, * Categorical label contains more than 32,000 characters .. versionadded:: 0.19.0 - + See Also -------- read_stata : Import Stata data files. From 85b7c20dcbd772368fa1ed65f3490694dbb2287d Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Mon, 15 Oct 2018 00:49:14 +0100 Subject: [PATCH 3/7] Fix whitespace on blank line 1918:1: W293 --- 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 275511961f779..26d3f2b723ed5 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1925,7 +1925,7 @@ def to_stata(self, fname, convert_dates=None, write_index=True, * Categorical label contains more than 32,000 characters .. versionadded:: 0.19.0 - + See Also -------- read_stata : Import Stata data files. From 13ad99439995ce62b264aed64b9eb3efb72558e2 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Fri, 26 Oct 2018 00:17:08 +0100 Subject: [PATCH 4/7] Incorporated comments --- pandas/core/frame.py | 31 +++++++------------------------ 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 26d3f2b723ed5..24c69701d1935 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1856,18 +1856,15 @@ def to_stata(self, fname, convert_dates=None, write_index=True, data_label=None, variable_labels=None, version=114, convert_strl=None): """ - Converting data frame object to Stata dta format. + Converting DataFrame object to Stata dta format. - Writes the Dataframe to a Stata dataset file. + Writes the DataFrame to a Stata dataset file. "dta" files contain a Stata dataset. Parameters ---------- - fname : path (string), buffer or path object - String, path object (pathlib.Path or py._path.local.LocalPath) or - object implementing a binary write() functions. If using a buffer - then the buffer will not be automatically closed after the file - data has been written. + fname : str, file descriptor or pathlib.Path + String or path to file which needs to be converted. convert_dates : dict Dictionary mapping columns containing datetime types to stata internal format to use when writing the dates. Options are 'tc', @@ -1909,10 +1906,6 @@ def to_stata(self, fname, convert_dates=None, write_index=True, .. versionadded:: 0.23.0 - Returns - ------ - Stata (dta) file or StataWriter. - Raises ------ NotImplementedError @@ -1934,22 +1927,12 @@ def to_stata(self, fname, convert_dates=None, write_index=True, Examples -------- - Converting dataframe with date column to Stata dta file - using the to_stata method. + Converting DataFrame to Stata "dta" file using the to_stata method. - >>> dates = pd.date_range(start='2018-01-01', periods=4) - >>> df = pd.DataFrame({'date': dates, - ... 'animal': ['falcon', 'parrot', 'falcon', + >>> df = pd.DataFrame({'animal': ['falcon', 'parrot', 'falcon', ... 'parrot'], - ... 'speed': [350, 18, 361, 15]}).set_index(['date', - ... 'animal']) + ... 'speed': [350, 18, 361, 15]}) >>> df.to_stata('animals.dta') - - Alternatively you can create an instance of the StataWriter class - - >>> StataWriter = pd.io.stata.StataWriter - >>> writer = StataWriter('animals.dta', df) - >>> writer.write_file() """ kwargs = {} if version not in (114, 117): From 8697ea8d81148a1450d1bbf2905a2cc0267a4683 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Tue, 6 Nov 2018 00:50:32 +0000 Subject: [PATCH 5/7] Minor changes --- ci/code_checks.sh | 2 +- pandas/core/frame.py | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/ci/code_checks.sh b/ci/code_checks.sh index f0772f72d63d4..86e7003681e98 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -148,7 +148,7 @@ if [[ -z "$CHECK" || "$CHECK" == "doctests" ]]; then MSG='Doctests frame.py' ; echo $MSG pytest -q --doctest-modules pandas/core/frame.py \ - -k"-axes -combine -itertuples -join -nunique -pivot_table -quantile -query -reindex -reindex_axis -replace -round -set_index -stack -to_stata" + -k"-axes -combine -itertuples -join -nunique -pivot_table -quantile -query -reindex -reindex_axis -replace -round -set_index -stack" RET=$(($RET + $?)) ; echo $MSG "DONE" MSG='Doctests series.py' ; echo $MSG diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 24c69701d1935..cc099295da041 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1863,8 +1863,11 @@ def to_stata(self, fname, convert_dates=None, write_index=True, Parameters ---------- - fname : str, file descriptor or pathlib.Path - String or path to file which needs to be converted. + fname : str, buffer or path object + String, path object (pathlib.Path or py._path.local.LocalPath) or + object implementing a binary write() function. If using a buffer + then the buffer will not be automatically closed after the file + data has been written. convert_dates : dict Dictionary mapping columns containing datetime types to stata internal format to use when writing the dates. Options are 'tc', @@ -1927,12 +1930,10 @@ def to_stata(self, fname, convert_dates=None, write_index=True, Examples -------- - Converting DataFrame to Stata "dta" file using the to_stata method. - >>> df = pd.DataFrame({'animal': ['falcon', 'parrot', 'falcon', ... 'parrot'], ... 'speed': [350, 18, 361, 15]}) - >>> df.to_stata('animals.dta') + >>> df.to_stata('animals.dta') # doctest: +SKIP """ kwargs = {} if version not in (114, 117): From dad93b95f032951933d58fd2689dfce157c05968 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Thu, 8 Nov 2018 00:11:58 +0000 Subject: [PATCH 6/7] Final --- pandas/core/frame.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index cc099295da041..c44343dcbc454 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1884,7 +1884,7 @@ def to_stata(self, fname, convert_dates=None, write_index=True, time_stamp : datetime A datetime to use as file creation date. Default is the current time. - data_label : str + data_label : str, optional A label for the data set. Must be 80 characters or smaller. variable_labels : dict Dictionary containing columns as keys and variable labels as @@ -1892,7 +1892,7 @@ def to_stata(self, fname, convert_dates=None, write_index=True, .. versionadded:: 0.19.0 - version : {114, 117} + version : {114, 117}, default 114 Version to use in the output dta file. Version 114 can be used read by Stata 10 and later. Version 117 can be read by Stata 13 or later. Version 114 limits string variables to 244 characters or From 64e02d0b9c670b7cc1879ff374d640d090788b48 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Tue, 20 Nov 2018 21:28:06 +0000 Subject: [PATCH 7/7] Final fixes --- 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 c44343dcbc454..572bb3668caf8 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1856,7 +1856,7 @@ def to_stata(self, fname, convert_dates=None, write_index=True, data_label=None, variable_labels=None, version=114, convert_strl=None): """ - Converting DataFrame object to Stata dta format. + Export DataFrame object to Stata dta format. Writes the DataFrame to a Stata dataset file. "dta" files contain a Stata dataset.