From 5692bbc821f02d15003e24d421de71c2e0a622d7 Mon Sep 17 00:00:00 2001 From: Ritchie Vink Date: Mon, 19 Jul 2021 10:34:19 +0200 Subject: [PATCH] python: allow empty series creation --- py-polars/polars/eager/series.py | 5 ++++- py-polars/tests/test_series.py | 13 +++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/py-polars/polars/eager/series.py b/py-polars/polars/eager/series.py index 615e0fb625ad..b04e5a1543f8 100644 --- a/py-polars/polars/eager/series.py +++ b/py-polars/polars/eager/series.py @@ -144,8 +144,11 @@ def __init__( raise ValueError( f"Constructing a Series with a dict is not supported for {values}" ) + elif values is None and dtype is None: + dtype = Float32 + values = [] elif values is None: - raise ValueError("Series values cannot be None.") + values = [] # castable to numpy if not isinstance(values, np.ndarray) and not nullable: diff --git a/py-polars/tests/test_series.py b/py-polars/tests/test_series.py index b64ce31a777c..c0041e6cb929 100644 --- a/py-polars/tests/test_series.py +++ b/py-polars/tests/test_series.py @@ -21,12 +21,10 @@ def test_init_inputs(): pl.Series([1, 2]) pl.Series(values=[1, 2]) + pl.Series() + pl.Series("a") # Bad inputs - with pytest.raises(ValueError): - pl.Series() - with pytest.raises(ValueError): - pl.Series("a") with pytest.raises(ValueError): pl.Series([1, 2, 3], [1, 2, 3]) with pytest.raises(ValueError): @@ -318,6 +316,13 @@ def test_iter(): assert sum(s) == 6 +def test_empty(): + a = pl.Series(dtype=pl.Int8) + assert a.dtype == pl.Int8 + a = pl.Series() + assert a.dtype == pl.Float32 + + def test_describe(): num_s = pl.Series([1, 2, 3]) float_s = pl.Series([1.3, 4.6, 8.9])