Repository: pandas-dev/pandas

Issue:
sparc test_read_dta10: KeyError: 0   self.dtyplist = [self.DTYPE_MAP[typ] for typ in typlist]
```
======================================================================
ERROR: test_read_dta10 (pandas.io.tests.test_stata.StataTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/yoh/deb/gits/pkg-exppsy/pandas/debian/tmp/usr/lib/python2.7/dist-packages/pandas/io/tests/test_stata.py", line 205, in test_read_dta10
    written_and_read_again = self.read_dta(path)
  File "/home/yoh/deb/gits/pkg-exppsy/pandas/debian/tmp/usr/lib/python2.7/dist-packages/pandas/io/tests/test_stata.py", line 41, in read_dta
    return read_stata(file, convert_dates=True)
  File "/home/yoh/deb/gits/pkg-exppsy/pandas/debian/tmp/usr/lib/python2.7/dist-packages/pandas/io/stata.py", line 38, in read_stata
    reader = StataReader(filepath_or_buffer, encoding)
  File "/home/yoh/deb/gits/pkg-exppsy/pandas/debian/tmp/usr/lib/python2.7/dist-packages/pandas/io/stata.py", line 305, in __init__
    self._read_header()
  File "/home/yoh/deb/gits/pkg-exppsy/pandas/debian/tmp/usr/lib/python2.7/dist-packages/pandas/io/stata.py", line 331, in _read_header
    self.dtyplist = [self.DTYPE_MAP[typ] for typ in typlist]
KeyError: 0
```

on 0.12.0~rc1+git43-g7b2eaa4-1



Discussion:
can u post all of the variables I that last line
typist and self.DTYPE_MAP

prob an endian issue 

```
-> self.dtyplist = [self.DTYPE_MAP[typ] for typ in typlist]
(Pdb) print self.DTYPE_MAP     
{1: 'a1', 2: 'a2', 3: 'a3', 4: 'a4', 5: 'a5', 6: 'a6', 7: 'a7', 8: 'a8', 9: 'a9', 10: 'a10', 11: 'a11', 12: 'a12', 13: 'a13', 14: 'a14', 15: 'a15', 16: 'a16', 17: 'a17', 18: 'a18', 19: 'a19', 20: 'a20', 21: 'a21', 22: 'a22', 23: 'a23', 24: 'a24', 25: 'a25', 26: 'a26', 27: 'a27', 28: 'a28', 29: 'a29', 30: 'a30', 31: 'a31', 32: 'a32', 33: 'a33', 34: 'a34', 35: 'a35', 36: 'a36', 37: 'a37', 38: 'a38', 39: 'a39', 40: 'a40', 41: 'a41', 42: 'a42', 43: 'a43', 44: 'a44', 45: 'a45', 46: 'a46', 47: 'a47', 48: 'a48', 49: 'a49', 50: 'a50', 51: 'a51', 52: 'a52', 53: 'a53', 54: 'a54', 55: 'a55', 56: 'a56', 57: 'a57', 58: 'a58', 59: 'a59', 60: 'a60', 61: 'a61', 62: 'a62', 63: 'a63', 64: 'a64', 65: 'a65', 66: 'a66', 67: 'a67', 68: 'a68', 69: 'a69', 70: 'a70', 71: 'a71', 72: 'a72', 73: 'a73', 74: 'a74', 75: 'a75', 76: 'a76', 77: 'a77', 78: 'a78', 79: 'a79', 80: 'a80', 81: 'a81', 82: 'a82', 83: 'a83', 84: 'a84', 85: 'a85', 86: 'a86', 87: 'a87', 88: 'a88', 89: 'a89', 90: 'a90', 91: 'a91', 92: 'a92', 93: 'a93', 94: 'a94', 95: 'a95', 96: 'a96', 97: 'a97', 98: 'a98', 99: 'a99', 100: 'a100', 101: 'a101', 102: 'a102', 103: 'a103', 104: 'a104', 105: 'a105', 106: 'a106', 107: 'a107', 108: 'a108', 109: 'a109', 110: 'a110', 111: 'a111', 112: 'a112', 113: 'a113', 114: 'a114', 115: 'a115', 116: 'a116', 117: 'a117', 118: 'a118', 119: 'a119', 120: 'a120', 121: 'a121', 122: 'a122', 123: 'a123', 124: 'a124', 125: 'a125', 126: 'a126', 127: 'a127', 128: 'a128', 129: 'a129', 130: 'a130', 131: 'a131', 132: 'a132', 133: 'a133', 134: 'a134', 135: 'a135', 136: 'a136', 137: 'a137', 138: 'a138', 139: 'a139', 140: 'a140', 141: 'a141', 142: 'a142', 143: 'a143', 144: 'a144', 145: 'a145', 146: 'a146', 147: 'a147', 148: 'a148', 149: 'a149', 150: 'a150', 151: 'a151', 152: 'a152', 153: 'a153', 154: 'a154', 155: 'a155', 156: 'a156', 157: 'a157', 158: 'a158', 159: 'a159', 160: 'a160', 161: 'a161', 162: 'a162', 163: 'a163', 164: 'a164', 165: 'a165', 166: 'a166', 167: 'a167', 168: 'a168', 169: 'a169', 170: 'a170', 171: 'a171', 172: 'a172', 173: 'a173', 174: 'a174', 175: 'a175', 176: 'a176', 177: 'a177', 178: 'a178', 179: 'a179', 180: 'a180', 181: 'a181', 182: 'a182', 183: 'a183', 184: 'a184', 185: 'a185', 186: 'a186', 187: 'a187', 188: 'a188', 189: 'a189', 190: 'a190', 191: 'a191', 192: 'a192', 193: 'a193', 194: 'a194', 195: 'a195', 196: 'a196', 197: 'a197', 198: 'a198', 199: 'a199', 200: 'a200', 201: 'a201', 202: 'a202', 203: 'a203', 204: 'a204', 205: 'a205', 206: 'a206', 207: 'a207', 208: 'a208', 209: 'a209', 210: 'a210', 211: 'a211', 212: 'a212', 213: 'a213', 214: 'a214', 215: 'a215', 216: 'a216', 217: 'a217', 218: 'a218', 219: 'a219', 220: 'a220', 221: 'a221', 222: 'a222', 223: 'a223', 224: 'a224', 225: 'a225', 226: 'a226', 227: 'a227', 228: 'a228', 229: 'a229', 230: 'a230', 231: 'a231', 232: 'a232', 233: 'a233', 234: 'a234', 235: 'a235', 236: 'a236', 237: 'a237', 238: 'a238', 239: 'a239', 240: 'a240', 241: 'a241', 242: 'a242', 243: 'a243', 244: 'a244', 251: <type 'numpy.int16'>, 252: <type 'numpy.int32'>, 253: <type 'numpy.int64'>, 254: <type 'numpy.float32'>, 255: <type 'numpy.float64'>}
(Pdb) print typlist
[253, 244, 244, 253, 255, 255, 105, 110, 100, 101, 120, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 115, 116, 114, 105, 110, 103, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 111, 98, 106, 101, 99, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
```


Resolving patch:
diff --git a/pandas/io/stata.py b/pandas/io/stata.py
--- a/pandas/io/stata.py
+++ b/pandas/io/stata.py
@@ -327,8 +327,16 @@ def _read_header(self):
             typlist = [ord(self.path_or_buf.read(1)) for i in range(self.nvar)]
         else:
             typlist = [self.OLD_TYPE_MAPPING[self._decode_bytes(self.path_or_buf.read(1))] for i in range(self.nvar)]
-        self.typlist = [self.TYPE_MAP[typ] for typ in typlist]
-        self.dtyplist = [self.DTYPE_MAP[typ] for typ in typlist]
+
+        try:
+            self.typlist = [self.TYPE_MAP[typ] for typ in typlist]
+        except:
+            raise ValueError("cannot convert stata types [{0}]".format(','.join(typlist)))
+        try:
+            self.dtyplist = [self.DTYPE_MAP[typ] for typ in typlist]
+        except:
+            raise ValueError("cannot convert stata dtypes [{0}]".format(','.join(typlist)))
+
         if self.format_version > 108:
             self.varlist = [self._null_terminate(self.path_or_buf.read(33)) for i in range(self.nvar)]
         else:


Test patch:
diff --git a/pandas/io/tests/test_stata.py b/pandas/io/tests/test_stata.py
--- a/pandas/io/tests/test_stata.py
+++ b/pandas/io/tests/test_stata.py
@@ -132,7 +132,7 @@ def test_read_dta4(self):
     def test_write_dta5(self):
         if not is_little_endian():
             raise nose.SkipTest("known failure of test_write_dta5 on non-little endian")
-        
+
         original = DataFrame([(np.nan, np.nan, np.nan, np.nan, np.nan)],
                              columns=['float_miss', 'double_miss', 'byte_miss', 'int_miss', 'long_miss'])
         original.index.name = 'index'
@@ -145,7 +145,7 @@ def test_write_dta5(self):
     def test_write_dta6(self):
         if not is_little_endian():
             raise nose.SkipTest("known failure of test_write_dta6 on non-little endian")
-        
+
         original = self.read_csv(self.csv3)
         original.index.name = 'index'
 
@@ -191,6 +191,9 @@ def test_read_dta9(self):
             )
 
     def test_read_dta10(self):
+        if not is_little_endian():
+            raise nose.SkipTest("known failure of test_write_dta10 on non-little endian")
+
         original = DataFrame(
             data=
             [