Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Add support for MaskedArray in Series constructor, with test #563

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+20 −0
Split
View
@@ -11,6 +11,7 @@
from numpy import nan, ndarray
import numpy as np
+import numpy.ma as ma
from pandas.core.common import (isnull, notnull, _is_bool_indexer,
_default_index, _maybe_upcast,
@@ -2045,6 +2046,11 @@ def remove_na(arr):
def _sanitize_array(data, index, dtype=None, copy=False,
raise_cast_failure=False):
+ if isinstance(data, ma.MaskedArray):
+ mask = ma.getmaskarray(data)
+ data = ma.copy(data)
+ data[mask] = np.nan
+
try:
subarr = np.array(data, dtype=dtype, copy=copy)
except (ValueError, TypeError):
@@ -10,6 +10,7 @@
from numpy import nan
import numpy as np
+import numpy.ma as ma
from pandas import Index, Series, TimeSeries, DataFrame, isnull, notnull
from pandas.core.index import MultiIndex
@@ -189,6 +190,19 @@ def test_constructor(self):
self.assertRaises(Exception, Series, np.random.randn(3, 3),
index=np.arange(3))
+ def test_constructor_maskedarray(self):
+ data = ma.masked_all((3,), dtype=float)
+ result = Series(data)
+ expected = Series([nan, nan, nan])
+ assert_series_equal(result, expected)
+
+ data[0] = 0.0
+ data[2] = 2.0
+ index = ['a', 'b', 'c']
+ result = Series(data, index=index)
+ expected = Series([0.0, nan, 2.0], index=index)
+ assert_series_equal(result, expected)
+
def test_constructor_default_index(self):
s = Series([0, 1, 2])
assert_almost_equal(s.index, np.arange(3))