mirror of
https://github.com/microsoft/qlib.git
synced 2026-07-13 15:56:57 +08:00
Make sepdf more like DataFrame (#1080)
This commit is contained in:
54
tests/misc/test_sepdf.py
Normal file
54
tests/misc/test_sepdf.py
Normal file
@@ -0,0 +1,54 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# Licensed under the MIT License.
|
||||
import unittest
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from qlib.contrib.data.utils.sepdf import SepDataFrame
|
||||
|
||||
|
||||
class SepDF(unittest.TestCase):
|
||||
def to_str(self, obj):
|
||||
return "".join(str(obj).split())
|
||||
|
||||
def test_index_data(self):
|
||||
|
||||
np.random.seed(42)
|
||||
|
||||
index = [
|
||||
np.array(["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"]),
|
||||
np.array(["one", "two", "one", "two", "one", "two", "one", "two"]),
|
||||
]
|
||||
|
||||
cols = [
|
||||
np.repeat(np.array(["g1", "g2"]), 2),
|
||||
np.arange(4),
|
||||
]
|
||||
df = pd.DataFrame(np.random.randn(8, 4), index=index, columns=cols)
|
||||
sdf = SepDataFrame(df_dict={"g2": df["g2"]}, join=None)
|
||||
sdf[("g2", 4)] = 3
|
||||
sdf["g1"] = df["g1"]
|
||||
exp = """
|
||||
{'g2': 2 3 4
|
||||
bar one 0.647689 1.523030 3
|
||||
two 1.579213 0.767435 3
|
||||
baz one -0.463418 -0.465730 3
|
||||
two -1.724918 -0.562288 3
|
||||
foo one -0.908024 -1.412304 3
|
||||
two 0.067528 -1.424748 3
|
||||
qux one -1.150994 0.375698 3
|
||||
two -0.601707 1.852278 3, 'g1': 0 1
|
||||
bar one 0.496714 -0.138264
|
||||
two -0.234153 -0.234137
|
||||
baz one -0.469474 0.542560
|
||||
two 0.241962 -1.913280
|
||||
foo one -1.012831 0.314247
|
||||
two 1.465649 -0.225776
|
||||
qux one -0.544383 0.110923
|
||||
two -0.600639 -0.291694}
|
||||
"""
|
||||
self.assertEqual(self.to_str(sdf._df_dict), self.to_str(exp))
|
||||
# self.assertEqual(self.to_str(data.tail()), self.to_str(res))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user