Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #218 from sdvillal/fixfromtranspose2
Browse files Browse the repository at this point in the history
More fixes when importing homogeneous pandas dataframes
  • Loading branch information
FrancescAlted committed Jul 14, 2015
2 parents e260ca7 + 3e43ec8 commit 4bbc307
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion RELEASE_NOTES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Changes from 0.9.0 to 0.10.0
- Various refactorings and cleanups. (#190 #198 #197 #199 #200)

- Fix bug creating carrays from transposed arrays without explicit dtype.
(#217 @sdvillal)
(#217 #218 @sdvillal)


Changes from 0.8.1 to 0.9.0
Expand Down
23 changes: 23 additions & 0 deletions bcolz/tests/test_carray.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,29 @@ def test_roundtrip_from_transpose2(self):
transposed_array = np.array([[0, 1, 2], [2, 1, 0]]).T
assert_array_equal(transposed_array, carray(transposed_array, dtype=transposed_array.dtype))

@unittest.skipIf(not bcolz.pandas_here, "cannot import pandas")
def test_roundtrip_from_dataframe1(self):
"""Testing `__init__` called without `dtype` and a dataframe over non-contiguous data."""
import pandas as pd
df = pd.DataFrame(data={
'a': np.arange(3),
'b': np.arange(3)[::-1]
})
assert_array_equal(df, carray(df, dtype=None))

@unittest.skipIf(not bcolz.pandas_here, "cannot import pandas")
def test_roundtrip_from_dataframe2(self):
"""Testing `__init__` called with `dtype` and a dataframe over non-contiguous data."""
import pandas as pd
df = pd.DataFrame(data={
'a': np.arange(3),
'b': np.arange(3)[::-1]
})
ca = carray(df, dtype=np.dtype(np.float))
assert_array_equal(df, ca)
self.assertEqual(ca.dtype, np.dtype(np.float),
msg='carray has been created with invalid dtype')

def test_dtype_None(self):
"""Testing `utils.to_ndarray` called without `dtype` and a non-contiguous (transposed) array."""
array = np.array([[0, 1, 2], [2, 1, 0]]).T
Expand Down
22 changes: 11 additions & 11 deletions bcolz/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,29 +105,29 @@ def to_ndarray(array, dtype, arrlen=None, safe=True):
if not safe:
return array

if dtype is None:
if not isinstance(array, np.ndarray):
return np.array(array)
dtype = array.dtype
if not isinstance(array, np.ndarray) and dtype is None:
array = np.array(array)

# Arrays with a 0 stride are special
if type(array) == np.ndarray and array.strides[0] == 0:
if type(array) == np.ndarray and len(array.strides) and array.strides[0] == 0:
if array.dtype != dtype.base:
raise TypeError("dtypes do not match")
return array

# Ensure that we have an ndarray of the correct dtype
if type(array) != np.ndarray or array.dtype != dtype.base:
try:
array = np.array(array, dtype=dtype.base)
except ValueError:
raise ValueError("cannot convert to an ndarray object")
if dtype is not None:
if type(array) != np.ndarray or array.dtype != dtype.base:
try:
array = np.array(array, dtype=dtype.base)
except ValueError:
raise ValueError("cannot convert to an ndarray object")

# We need a contiguous array
if not array.flags.contiguous:
array = array.copy()

# We treat scalars like undimensional arrays
if len(array.shape) == 0:
# We treat scalars like undimensional arrays
array.shape = (1,)

# Check if we need a broadcast
Expand Down

0 comments on commit 4bbc307

Please sign in to comment.