From cd77f5472eff89f29e3b1f4648c7a0f648aee829 Mon Sep 17 00:00:00 2001 From: Dieter Vandenbussche Date: Sun, 1 Jan 2012 21:09:44 -0500 Subject: [PATCH] Add support for MaskedArray in Series constructor, with test --- pandas/core/series.py | 6 ++++++ pandas/tests/test_series.py | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/pandas/core/series.py b/pandas/core/series.py index 709d525d8c..3048c7b3a5 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -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): diff --git a/pandas/tests/test_series.py b/pandas/tests/test_series.py index f09fd6346f..d5c836b8bd 100644 --- a/pandas/tests/test_series.py +++ b/pandas/tests/test_series.py @@ -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))