Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,15 @@ def ensure_float(arr):

return arr

def assert_ndarray_endianess(ar):
import sys
little_endian = (sys.byteorder == 'little')
if not isinstance(ar,np.ndarray):
return

if ((ar.dtype.byteorder == '>' and little_endian) or
(ar.dtype.byteorder == '<' and not little_endian)):
raise ValueError(u"Non-native byte order not supported")

def _mut_exclusive(arg1, arg2):
if arg1 is not None and arg2 is not None:
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,9 @@ def __init__(self, data=None, index=None, columns=None, dtype=None,
if data is None:
data = {}

for x in (data, index, columns):
com.assert_ndarray_endianess(x)

if isinstance(data, DataFrame):
data = data._data

Expand Down
3 changes: 3 additions & 0 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ class Index(np.ndarray):

def __new__(cls, data, dtype=None, copy=False, name=None):
if isinstance(data, np.ndarray):

com.assert_ndarray_endianess(data)

if issubclass(data.dtype.type, np.datetime64):
from pandas.tseries.index import DatetimeIndex
result = DatetimeIndex(data, copy=copy, name=name)
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ def __init__(self, data=None, items=None, major_axis=None, minor_axis=None,
if data is None:
data = {}

for x in (data, items, major_axis,minor_axis):
com.assert_ndarray_endianess(x)

passed_axes = [items, major_axis, minor_axis]
axes = None
if isinstance(data, BlockManager):
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,9 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
input data
copy : boolean, default False
"""
pass

for x in (data, index):
com.assert_ndarray_endianess(x)

@property
def _constructor(self):
Expand Down
3 changes: 3 additions & 0 deletions pandas/sparse/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ def __init__(self, data=None, index=None, columns=None,
self.default_kind = default_kind
self.default_fill_value = default_fill_value

for x in (data, index, columns):
com.assert_ndarray_endianess(x)

if isinstance(data, dict):
sdict, columns, index = self._init_dict(data, index, columns)
elif isinstance(data, (np.ndarray, list)):
Expand Down
3 changes: 3 additions & 0 deletions pandas/sparse/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ def __init__(self, frames, items=None, major_axis=None, minor_axis=None,
self.default_fill_value = fill_value = default_fill_value
self.default_kind = kind = default_kind

for x in (items, major_axis,minor_axis):
com.assert_ndarray_endianess(x)

# pre-filter, if necessary
if items is None:
items = Index(sorted(frames.keys()))
Expand Down
7 changes: 5 additions & 2 deletions pandas/sparse/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from pandas.core.index import Index, _ensure_index
from pandas.core.series import Series, TimeSeries, _maybe_match_name
from pandas.core.frame import DataFrame
import pandas.core.common as common
import pandas.core.common as com
import pandas.core.datetools as datetools

from pandas.util import py3compat
Expand Down Expand Up @@ -82,6 +82,9 @@ class SparseSeries(SparseArray, Series):
def __new__(cls, data, index=None, sparse_index=None, kind='block',
fill_value=None, name=None, copy=False):

for x in (data, index):
com.assert_ndarray_endianess(x)

is_sparse_array = isinstance(data, SparseArray)
if fill_value is None:
if is_sparse_array:
Expand Down Expand Up @@ -412,7 +415,7 @@ def reindex(self, index=None, method=None, copy=True, limit=None):

new_index, fill_vec = self.index.reindex(index, method=method,
limit=limit)
new_values = common.take_1d(self.values, fill_vec)
new_values = com.take_1d(self.values, fill_vec)
return SparseSeries(new_values, index=new_index,
fill_value=self.fill_value, name=self.name)

Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@

from pandas.util import py3compat

def test_assert_endianess():
little_endian = (sys.byteorder == 'little')
if little_endian:
arr = np.array([1], dtype='>i8')
else:
arr = np.array([1], dtype='<i8')

try:
DataFrame(arr)
except:
pass
else:
assert False,"did not raise ValueError on wrong endianess"

def test_is_sequence():
is_seq=com._is_sequence
assert(is_seq((1,2)))
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,10 @@ def setUp(self):
self.simple = DataFrame(arr, columns=['one', 'two', 'three'],
index=['a', 'b', 'c'])

def test_wrong_endianess_caught(self):
arr = np.array([1], dtype='>i8')
self.assertRaises(ValueError,DataFrame,arr)

def test_get_axis(self):
self.assert_(DataFrame._get_axis_name(0) == 'index')
self.assert_(DataFrame._get_axis_name(1) == 'columns')
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def setUp(self):
self.empty = Index([])
self.tuples = Index(zip(['foo', 'bar', 'baz'], [1, 2, 3]))

def test_wrong_endianess_caught(self):
arr = np.array([1], dtype='>i8')
self.assertRaises(ValueError,Index,arr)

def test_hash_error(self):
self.assertRaises(TypeError, hash, self.strIndex)

Expand Down Expand Up @@ -864,6 +868,7 @@ def setUp(self):
labels=[major_labels, minor_labels],
names=['first', 'second'])


def test_constructor_single_level(self):
single_level = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux']],
labels=[[0, 1, 2, 3]],
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ def setUp(self):

self.empty = Series([], index=[])

def test_wrong_endianess_caught(self):
arr = np.array([1], dtype='>i8')
self.assertRaises(ValueError,Series,arr)

def test_constructor(self):
# Recognize TimeSeries
self.assert_(isinstance(self.ts, TimeSeries))
Expand Down