-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Add support for writing variable labels to Stata files #13631
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,27 @@ | ||
# -*- coding: utf-8 -*- | ||
# pylint: disable=E1101 | ||
|
||
from datetime import datetime | ||
import datetime as dt | ||
import os | ||
import warnings | ||
import nose | ||
import struct | ||
import sys | ||
import warnings | ||
from datetime import datetime | ||
from distutils.version import LooseVersion | ||
|
||
import nose | ||
import numpy as np | ||
|
||
import pandas as pd | ||
import pandas.util.testing as tm | ||
from pandas import compat | ||
from pandas.compat import iterkeys | ||
from pandas.core.frame import DataFrame, Series | ||
from pandas.types.common import is_categorical_dtype | ||
from pandas.tslib import NaT | ||
from pandas.io.parsers import read_csv | ||
from pandas.io.stata import (read_stata, StataReader, InvalidColumnName, | ||
PossiblePrecisionLoss, StataMissingValue) | ||
import pandas.util.testing as tm | ||
from pandas.tslib import NaT | ||
from pandas import compat | ||
|
||
|
||
class TestStata(tm.TestCase): | ||
|
@@ -1113,6 +1113,58 @@ def test_read_chunks_columns(self): | |
tm.assert_frame_equal(from_frame, chunk, check_dtype=False) | ||
pos += chunksize | ||
|
||
def test_write_variable_labels(self): | ||
# GH 13631, add support for writing variable labels | ||
original = pd.DataFrame({'a': [1, 2, 3, 4], | ||
'b': [1.0, 3.0, 27.0, 81.0], | ||
'c': ['Atlanta', 'Birmingham', | ||
'Cincinnati', 'Detroit']}) | ||
original.index.name = 'index' | ||
variable_labels = {'a': 'City Rank', 'b': 'City Exponent', 'c': 'City'} | ||
with tm.ensure_clean() as path: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you need to close the StataReader (I think we have an ensure_ for that somewhere) |
||
original.to_stata(path, variable_labels=variable_labels) | ||
with StataReader(path) as sr: | ||
read_labels = sr.variable_labels() | ||
expected_labels = {'index': '', | ||
'a': 'City Rank', | ||
'b': 'City Exponent', | ||
'c': 'City'} | ||
tm.assert_equal(read_labels, expected_labels) | ||
|
||
variable_labels['index'] = 'The Index' | ||
with tm.ensure_clean() as path: | ||
original.to_stata(path, variable_labels=variable_labels) | ||
with StataReader(path) as sr: | ||
read_labels = sr.variable_labels() | ||
tm.assert_equal(read_labels, variable_labels) | ||
|
||
def test_write_variable_label_errors(self): | ||
original = pd.DataFrame({'a': [1, 2, 3, 4], | ||
'b': [1.0, 3.0, 27.0, 81.0], | ||
'c': ['Atlanta', 'Birmingham', | ||
'Cincinnati', 'Detroit']}) | ||
values = [u'\u03A1', u'\u0391', | ||
u'\u039D', u'\u0394', | ||
u'\u0391', u'\u03A3'] | ||
|
||
variable_labels_utf8 = {'a': 'City Rank', | ||
'b': 'City Exponent', | ||
'c': u''.join(values)} | ||
|
||
with tm.assertRaises(ValueError): | ||
with tm.ensure_clean() as path: | ||
original.to_stata(path, variable_labels=variable_labels_utf8) | ||
|
||
variable_labels_long = {'a': 'City Rank', | ||
'b': 'City Exponent', | ||
'c': 'A very, very, very long variable label ' | ||
'that is too long for Stata which means ' | ||
'that it has more than 80 characters'} | ||
|
||
with tm.assertRaises(ValueError): | ||
with tm.ensure_clean() as path: | ||
original.to_stata(path, variable_labels=variable_labels_long) | ||
|
||
|
||
if __name__ == '__main__': | ||
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'], | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where these new entries just left out before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Arguments were present but uncodumented. Mentioned in one fo the issues this closes.