From 28046c75f48ee5b92195a65fc60ea66c11427559 Mon Sep 17 00:00:00 2001 From: John Reynolds Date: Fri, 13 Nov 2026 04:56:46 -0800 Subject: [PATCH] fix(data): preserve single-symbol concat values --- tests/test_data.py | 87 +++++++++++++++++++++++++++++++++++++++++++ vectorbt/data/base.py | 7 +++- 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/tests/test_data.py b/tests/test_data.py index 60326fdd..4071f5a1 100644 --- a/tests/test_data.py +++ b/tests/test_data.py @@ -871,6 +871,93 @@ def test_update(self): ), ) + @pytest.mark.parametrize("as_frame", [False, True]) + @pytest.mark.parametrize( + "values,dtype,expected_dtype", + [ + ([1704067200000000001, 1704067200000000003], "int64", "int64"), + ([2**64 - 3, 2**64 - 1], "uint64", "uint64"), + ([1, 2], "int16", "int16"), + ([1.5, np.nan], "float32", "float32"), + ([1.5, np.nan], "float64", "float64"), + ([np.nan, np.nan], "float64", "float64"), + ([True, False], "bool", "bool"), + ([2**53 + 1, 2**53 + 3], object, "int64"), + (["a", pd.NA], "string", "string"), + ([2**53 + 1, pd.NA], "Int64", "Int64"), + ([True, pd.NA], "boolean", "boolean"), + ([pd.Timestamp("2024-01-01"), pd.NaT], "datetime64[ns]", "datetime64[ns]"), + ([pd.Timestamp("2024-01-01", tz="UTC"), pd.NaT], "datetime64[ns, UTC]", "datetime64[ns, UTC]"), + ([pd.Timedelta(1, "ns"), pd.NaT], "timedelta64[ns]", "timedelta64[ns]"), + (["a", "b"], "category", "category"), + ([], "int64", "int64"), + ], + ) + def test_concat_single_symbol_values(self, values, dtype, expected_dtype, as_frame): + # Derive the expected values independently, including the retained object inference. + index = pd.Index(["tick1", "tick2"][: len(values)], name="tick") + source = pd.Series(values, index=index, name="feature", dtype=dtype) + data = vbt.Data.from_data({"A": source.to_frame() if as_frame else source}) + result = data.concat()["feature"] + expected = pd.Series(values, index=index, name="A", dtype=expected_dtype) + assert result.dtype == expected.dtype + pd.testing.assert_series_equal(result, expected, check_exact=True) + pd.testing.assert_series_equal(source, pd.Series(values, index=index, name="feature", dtype=dtype)) + + @pytest.mark.parametrize("as_frame", [False, True]) + @pytest.mark.parametrize("infer_string", [False, True]) + def test_concat_single_symbol_inference(self, as_frame, infer_string): + # Preserve pandas' final object inference under either supported string-inference setting. + with pd.option_context("future.infer_string", infer_string): + source = pd.Series(["a", None], name="feature", dtype=object) + data = vbt.Data.from_data({"A": source.to_frame() if as_frame else source}) + result = data.concat()["feature"] + expected = pd.Series(["a", None], name="A", dtype="str" if infer_string else object) + assert result.dtype == expected.dtype + pd.testing.assert_series_equal(result, expected) + + @pytest.mark.parametrize("as_frame", [False, True]) + @pytest.mark.parametrize("symbol,feature", [(None, "id"), (("venue", "A"), ("feed", "id")), ("A", None)]) + def test_concat_single_symbol_labels(self, symbol, feature, as_frame): + # An explicit None symbol must replace the feature name, rather than inherit it. + index = pd.MultiIndex.from_tuples([("day1", 1), ("day1", 2)], names=["day", "tick"]) + source = pd.Series([3, 4], index=index, name=feature) + data = vbt.Data.from_data({symbol: source.to_frame(name=feature) if as_frame else source}) + result = data.concat(level_name="instrument") + assert list(result) == [feature] + pd.testing.assert_series_equal(result[feature], pd.Series([3, 4], index=index, name=symbol)) + + @pytest.mark.parametrize("as_frame", [False, True]) + @pytest.mark.parametrize("dtype", ["int64", "Int64"]) + def test_concat_single_symbol_ownership(self, as_frame, dtype): + source = pd.Series([2**53 + 1, 2**53 + 3], name="id", dtype=dtype) + data = vbt.Data.from_data({"A": source.to_frame() if as_frame else source}) + result = data.concat()["id"] + stored = data.data["A"]["id"] if as_frame else data.data["A"] + + # Array-level writes also detect shared buffers that pandas copy-on-write could hide. + result.array[0] = 7 + pd.testing.assert_series_equal(stored, pd.Series([2**53 + 1, 2**53 + 3], name="id", dtype=dtype)) + stored.array[1] = 9 + pd.testing.assert_series_equal(result, pd.Series([7, 2**53 + 3], name="A", dtype=dtype)) + + @pytest.mark.parametrize("as_frame", [False, True]) + def test_concat_single_symbol_export(self, as_frame): + # Neighboring nanosecond timestamps must remain distinct in an actual CSV export. + index = pd.Index(["tick1", "tick2"], name="tick") + source = pd.Series([1704067200000000001, 1704067200000000003], index=index, name="EventTime") + payload = source.to_frame() if as_frame else source + data = vbt.Data.from_data({"A": payload}) + result = data.concat()["EventTime"] + assert [int(value) for value in result] == [1704067200000000001, 1704067200000000003] + assert result.nunique() == 2 + # Keep the exact CSV oracle independent of the platform's default line ending. + assert result.to_csv(lineterminator="\n") == "tick,A\ntick1,1704067200000000001\ntick2,1704067200000000003\n" + if as_frame: + pd.testing.assert_frame_equal(data.get(), payload) + else: + pd.testing.assert_series_equal(data.get(), payload) + def test_concat(self): index = pd.DatetimeIndex( [ diff --git a/vectorbt/data/base.py b/vectorbt/data/base.py index fc74018f..e931e70c 100644 --- a/vectorbt/data/base.py +++ b/vectorbt/data/base.py @@ -695,7 +695,7 @@ def concat(self, level_name: str = "symbol") -> tp.Data: if len(self.symbols) > 1: new_data = {c: pd.DataFrame(index=index, columns=pd.Index(self.symbols, name=level_name)) for c in columns} else: - new_data = {c: pd.Series(index=index, name=self.symbols[0]) for c in columns} + new_data = {} for c in columns: for s in self.symbols: if isinstance(self.data[s], pd.Series): @@ -705,7 +705,10 @@ def concat(self, level_name: str = "symbol") -> tp.Data: if len(self.symbols) > 1: new_data[c].loc[:, s] = col_data else: - new_data[c].loc[:] = col_data + # A float placeholder can round integers or reject non-numeric columns. + new_data[c] = pd.Series(col_data, index=index, copy=True) + # Assign explicitly so a None symbol does not inherit the feature name. + new_data[c].name = s for c in columns: new_data[c] = new_data[c].infer_objects() return new_data