Skip to content

Commit 52a30d9

Browse files
ksheddenjreback
authored andcommitted
Closes pandas-dev#9795 (Stata writer changes input frame)
Add note to release notes
1 parent 3149a1e commit 52a30d9

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

doc/source/whatsnew/v0.16.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Bug Fixes
6666

6767
- Bug in ``scatter_matrix`` draws unexpected axis ticklabels (:issue:`5662`)
6868

69-
69+
- Fixed bug in ``StataWriter`` resulting in changes to input ``DataFrame`` upon save (:issue:`9795`).
7070

7171

7272
- Bug in ``transform`` causing length mismatch when null entries were present and a fast aggregator was being used (:issue:`9697`)

pandas/io/stata.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1885,6 +1885,8 @@ def _prepare_pandas(self, data):
18851885
#NOTE: we might need a different API / class for pandas objects so
18861886
# we can set different semantics - handle this with a PR to pandas.io
18871887

1888+
data = data.copy()
1889+
18881890
if self._write_index:
18891891
data = data.reset_index()
18901892

@@ -2013,7 +2015,7 @@ def _write_variable_labels(self, labels=None):
20132015
self._write(_pad_bytes("", 81))
20142016

20152017
def _prepare_data(self):
2016-
data = self.data.copy()
2018+
data = self.data
20172019
typlist = self.typlist
20182020
convert_dates = self._convert_dates
20192021
# 1. Convert dates

pandas/io/tests/test_stata.py

+9
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,15 @@ def test_stata_doc_examples(self):
290290
df = DataFrame(np.random.randn(10, 2), columns=list('AB'))
291291
df.to_stata(path)
292292

293+
def test_write_preserves_original(self):
294+
# 9795
295+
np.random.seed(423)
296+
df = pd.DataFrame(np.random.randn(5,4), columns=list('abcd'))
297+
df.ix[2, 'a':'c'] = np.nan
298+
df_copy = df.copy()
299+
df.to_stata('test.dta', write_index=False)
300+
tm.assert_frame_equal(df, df_copy)
301+
293302
def test_encoding(self):
294303

295304
# GH 4626, proper encoding handling

0 commit comments

Comments
 (0)