Example #1
0
    def test_copy(self):
        """Check inplace/copy behavior of link_df, link_df_iter"""
        # One 1D stepper
        N = 5
        f = DataFrame({"x": np.arange(N), "y": np.ones(N), "frame": np.arange(N)})
        f_inplace = f.copy()
        expected = f.copy()
        expected["particle"] = np.zeros(N)

        # Should add particle column in-place
        # UNLESS diagnostics are enabled (or input dataframe is not writeable)
        actual = self.link_df(f_inplace, 5)
        assert_frame_equal(actual, expected)
        if self.do_diagnostics:
            assert "particle" not in f_inplace.columns
        else:
            assert_frame_equal(actual, f_inplace)

        # When DataFrame is actually a view, link_df should produce a warning
        # and then copy the DataFrame. This only happens for pandas >= 0.16.
        if StrictVersion(pd.__version__) >= StrictVersion("0.16.0"):
            with assert_produces_warning(UserWarning):
                actual = self.link_df(f[f["frame"] > 0], 5)
            assert "particle" not in f.columns

        # Should copy
        actual = self.link_df(f, 5, copy_features=True)
        assert_frame_equal(actual, expected)
        assert "particle" not in f.columns

        # Should copy
        actual_iter = self.link_df_iter(f, 5, hash_size=(10, 2))
        assert_frame_equal(actual_iter, expected)
        assert "particle" not in f.columns
Example #2
0
    def test_assign(self):
        df = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
        original = df.copy()
        result = df.assign(C=df.B / df.A)
        expected = df.copy()
        expected["C"] = [4, 2.5, 2]
        assert_frame_equal(result, expected)

        # lambda syntax
        result = df.assign(C=lambda x: x.B / x.A)
        assert_frame_equal(result, expected)

        # original is unmodified
        assert_frame_equal(df, original)

        # Non-Series array-like
        result = df.assign(C=[4, 2.5, 2])
        assert_frame_equal(result, expected)
        # original is unmodified
        assert_frame_equal(df, original)

        result = df.assign(B=df.B / df.A)
        expected = expected.drop("B", axis=1).rename(columns={"C": "B"})
        assert_frame_equal(result, expected)

        # overwrite
        result = df.assign(A=df.A + df.B)
        expected = df.copy()
        expected["A"] = [5, 7, 9]
        assert_frame_equal(result, expected)

        # lambda
        result = df.assign(A=lambda x: x.A + x.B)
        assert_frame_equal(result, expected)
    def test_append_empty_dataframe(self):

        # Empty df append empty df
        df1 = DataFrame([])
        df2 = DataFrame([])
        result = df1.append(df2)
        expected = df1.copy()
        assert_frame_equal(result, expected)

        # Non-empty df append empty df
        df1 = DataFrame(np.random.randn(5, 2))
        df2 = DataFrame()
        result = df1.append(df2)
        expected = df1.copy()
        assert_frame_equal(result, expected)

        # Empty df with columns append empty df
        df1 = DataFrame(columns=["bar", "foo"])
        df2 = DataFrame()
        result = df1.append(df2)
        expected = df1.copy()
        assert_frame_equal(result, expected)

        # Non-Empty df with columns append empty df
        df1 = DataFrame(np.random.randn(5, 2), columns=["bar", "foo"])
        df2 = DataFrame()
        result = df1.append(df2)
        expected = df1.copy()
        assert_frame_equal(result, expected)
    def test_drop_names(self):
        df = DataFrame([[1, 2, 3], [3, 4, 5], [5, 6, 7]], index=["a", "b", "c"], columns=["d", "e", "f"])
        df.index.name, df.columns.name = "first", "second"
        df_dropped_b = df.drop("b")
        df_dropped_e = df.drop("e", axis=1)
        df_inplace_b, df_inplace_e = df.copy(), df.copy()
        df_inplace_b.drop("b", inplace=True)
        df_inplace_e.drop("e", axis=1, inplace=True)
        for obj in (df_dropped_b, df_dropped_e, df_inplace_b, df_inplace_e):
            self.assertEqual(obj.index.name, "first")
            self.assertEqual(obj.columns.name, "second")
        self.assertEqual(list(df.columns), ["d", "e", "f"])

        self.assertRaises(ValueError, df.drop, ["g"])
        self.assertRaises(ValueError, df.drop, ["g"], 1)

        # errors = 'ignore'
        dropped = df.drop(["g"], errors="ignore")
        expected = Index(["a", "b", "c"], name="first")
        self.assert_index_equal(dropped.index, expected)

        dropped = df.drop(["b", "g"], errors="ignore")
        expected = Index(["a", "c"], name="first")
        self.assert_index_equal(dropped.index, expected)

        dropped = df.drop(["g"], axis=1, errors="ignore")
        expected = Index(["d", "e", "f"], name="second")
        self.assert_index_equal(dropped.columns, expected)

        dropped = df.drop(["d", "g"], axis=1, errors="ignore")
        expected = Index(["e", "f"], name="second")
        self.assert_index_equal(dropped.columns, expected)
Example #5
0
    def test_copy(self):
        """Check inplace/copy behavior of link_df, link_df_iter"""
        # One 1D stepper
        N = 5
        f = DataFrame({"x": np.arange(N), "y": np.ones(N), "frame": np.arange(N)})
        f_inplace = f.copy()
        expected = f.copy()
        expected["particle"] = np.zeros(N)

        # Should add particle column in-place
        # UNLESS diagnostics are enabled
        actual = self.link_df(f_inplace, 5)
        assert_frame_equal(actual, expected)
        if self.do_diagnostics:
            assert "particle" not in f_inplace.columns
        else:
            assert_frame_equal(actual, f_inplace)

        # Should copy
        actual = self.link_df(f, 5, copy_features=True)
        assert_frame_equal(actual, expected)
        assert "particle" not in f.columns

        # Should copy
        actual_iter = self.link_df_iter(f, 5, hash_size=(10, 2))
        assert_frame_equal(actual_iter, expected)
        assert "particle" not in f.columns
Example #6
0
    def test_interp_alt_scipy(self):
        tm._skip_if_no_scipy()
        df = DataFrame({"A": [1, 2, np.nan, 4, 5, np.nan, 7], "C": [1, 2, 3, 5, 8, 13, 21]})
        result = df.interpolate(method="barycentric")
        expected = df.copy()
        expected.ix[2, "A"] = 3
        expected.ix[5, "A"] = 6
        assert_frame_equal(result, expected)

        result = df.interpolate(method="barycentric", downcast="infer")
        assert_frame_equal(result, expected.astype(np.int64))

        result = df.interpolate(method="krogh")
        expectedk = df.copy()
        expectedk["A"] = expected["A"]
        assert_frame_equal(result, expectedk)

        _skip_if_no_pchip()
        import scipy

        result = df.interpolate(method="pchip")
        expected.ix[2, "A"] = 3

        if LooseVersion(scipy.__version__) >= "0.17.0":
            expected.ix[5, "A"] = 6.0
        else:
            expected.ix[5, "A"] = 6.125

        assert_frame_equal(result, expected)
Example #7
0
    def test_stack_mixed_levels(self):
        columns = MultiIndex.from_tuples(
            [("A", "cat", "long"), ("B", "cat", "long"), ("A", "dog", "short"), ("B", "dog", "short")],
            names=["exp", "animal", "hair_length"],
        )
        df = DataFrame(randn(4, 4), columns=columns)

        animal_hair_stacked = df.stack(level=["animal", "hair_length"])
        exp_hair_stacked = df.stack(level=["exp", "hair_length"])

        # GH #8584: Need to check that stacking works when a number
        # is passed that is both a level name and in the range of
        # the level numbers
        df2 = df.copy()
        df2.columns.names = ["exp", "animal", 1]
        assert_frame_equal(df2.stack(level=["animal", 1]), animal_hair_stacked, check_names=False)
        assert_frame_equal(df2.stack(level=["exp", 1]), exp_hair_stacked, check_names=False)

        # When mixed types are passed and the ints are not level
        # names, raise
        self.assertRaises(ValueError, df2.stack, level=["animal", 0])

        # GH #8584: Having 0 in the level names could raise a
        # strange error about lexsort depth
        df3 = df.copy()
        df3.columns.names = ["exp", "animal", 0]
        assert_frame_equal(df3.stack(level=["animal", 0]), animal_hair_stacked, check_names=False)
Example #8
0
    def test_inplace_ops_identity(self):

        # GH 5104
        # make sure that we are actually changing the object
        s_orig = Series([1, 2, 3])
        df_orig = DataFrame(np.random.randint(0, 5, size=10).reshape(-1, 5))

        # no dtype change
        s = s_orig.copy()
        s2 = s
        s += 1
        assert_series_equal(s, s2)
        assert_series_equal(s_orig + 1, s)
        self.assertIs(s, s2)
        self.assertIs(s._data, s2._data)

        df = df_orig.copy()
        df2 = df
        df += 1
        assert_frame_equal(df, df2)
        assert_frame_equal(df_orig + 1, df)
        self.assertIs(df, df2)
        self.assertIs(df._data, df2._data)

        # dtype change
        s = s_orig.copy()
        s2 = s
        s += 1.5
        assert_series_equal(s, s2)
        assert_series_equal(s_orig + 1.5, s)

        df = df_orig.copy()
        df2 = df
        df += 1.5
        assert_frame_equal(df, df2)
        assert_frame_equal(df_orig + 1.5, df)
        self.assertIs(df, df2)
        self.assertIs(df._data, df2._data)

        # mixed dtype
        arr = np.random.randint(0, 10, size=5)
        df_orig = DataFrame({"A": arr.copy(), "B": "foo"})
        df = df_orig.copy()
        df2 = df
        df["A"] += 1
        expected = DataFrame({"A": arr.copy() + 1, "B": "foo"})
        assert_frame_equal(df, expected)
        assert_frame_equal(df2, expected)
        self.assertIs(df._data, df2._data)

        df = df_orig.copy()
        df2 = df
        df["A"] += 1.5
        expected = DataFrame({"A": arr.copy() + 1.5, "B": "foo"})
        assert_frame_equal(df, expected)
        assert_frame_equal(df2, expected)
        self.assertIs(df._data, df2._data)
Example #9
0
    def test_dropna(self):
        df = DataFrame(np.random.randn(6, 4))
        df[2][:2] = nan

        dropped = df.dropna(axis=1)
        expected = df.ix[:, [0, 1, 3]]
        inp = df.copy()
        inp.dropna(axis=1, inplace=True)
        assert_frame_equal(dropped, expected)
        assert_frame_equal(inp, expected)

        dropped = df.dropna(axis=0)
        expected = df.ix[lrange(2, 6)]
        inp = df.copy()
        inp.dropna(axis=0, inplace=True)
        assert_frame_equal(dropped, expected)
        assert_frame_equal(inp, expected)

        # threshold
        dropped = df.dropna(axis=1, thresh=5)
        expected = df.ix[:, [0, 1, 3]]
        inp = df.copy()
        inp.dropna(axis=1, thresh=5, inplace=True)
        assert_frame_equal(dropped, expected)
        assert_frame_equal(inp, expected)

        dropped = df.dropna(axis=0, thresh=4)
        expected = df.ix[lrange(2, 6)]
        inp = df.copy()
        inp.dropna(axis=0, thresh=4, inplace=True)
        assert_frame_equal(dropped, expected)
        assert_frame_equal(inp, expected)

        dropped = df.dropna(axis=1, thresh=4)
        assert_frame_equal(dropped, df)

        dropped = df.dropna(axis=1, thresh=3)
        assert_frame_equal(dropped, df)

        # subset
        dropped = df.dropna(axis=0, subset=[0, 1, 3])
        inp = df.copy()
        inp.dropna(axis=0, subset=[0, 1, 3], inplace=True)
        assert_frame_equal(dropped, df)
        assert_frame_equal(inp, df)

        # all
        dropped = df.dropna(axis=1, how="all")
        assert_frame_equal(dropped, df)

        df[2] = nan
        dropped = df.dropna(axis=1, how="all")
        expected = df.ix[:, [0, 1, 3]]
        assert_frame_equal(dropped, expected)

        # bad input
        self.assertRaises(ValueError, df.dropna, axis=3)
Example #10
0
    def test_interp_inplace(self):
        df = DataFrame({"a": [1.0, 2.0, np.nan, 4.0]})
        expected = DataFrame({"a": [1.0, 2.0, 3.0, 4.0]})
        result = df.copy()
        result["a"].interpolate(inplace=True)
        assert_frame_equal(result, expected)

        result = df.copy()
        result["a"].interpolate(inplace=True, downcast="infer")
        assert_frame_equal(result, expected.astype("int64"))
Example #11
0
 def test_regex_replace_regex_list_to_numeric(self):
     mix = {"a": lrange(4), "b": list("ab.."), "c": ["a", "b", nan, "d"]}
     df = DataFrame(mix)
     res = df.replace([r"\s*\.\s*", "b"], 0, regex=True)
     res2 = df.copy()
     res2.replace([r"\s*\.\s*", "b"], 0, regex=True, inplace=True)
     res3 = df.copy()
     res3.replace(regex=[r"\s*\.\s*", "b"], value=0, inplace=True)
     expec = DataFrame({"a": mix["a"], "b": ["a", 0, 0, 0], "c": ["a", 0, nan, "d"]})
     assert_frame_equal(res, expec)
     assert_frame_equal(res2, expec)
     assert_frame_equal(res3, expec)
Example #12
0
 def test_regex_replace_str_to_numeric(self):
     # what happens when you try to replace a numeric value with a regex?
     mix = {"a": lrange(4), "b": list("ab.."), "c": ["a", "b", nan, "d"]}
     df = DataFrame(mix)
     res = df.replace(r"\s*\.\s*", 0, regex=True)
     res2 = df.copy()
     res2.replace(r"\s*\.\s*", 0, inplace=True, regex=True)
     res3 = df.copy()
     res3.replace(regex=r"\s*\.\s*", value=0, inplace=True)
     expec = DataFrame({"a": mix["a"], "b": ["a", "b", 0, 0], "c": mix["c"]})
     assert_frame_equal(res, expec)
     assert_frame_equal(res2, expec)
     assert_frame_equal(res3, expec)
Example #13
0
    def test_replace_input_formats(self):
        # both dicts
        to_rep = {"A": np.nan, "B": 0, "C": ""}
        values = {"A": 0, "B": -1, "C": "missing"}
        df = DataFrame({"A": [np.nan, 0, np.inf], "B": [0, 2, 5], "C": ["", "asdf", "fd"]})
        filled = df.replace(to_rep, values)
        expected = {}
        for k, v in compat.iteritems(df):
            expected[k] = v.replace(to_rep[k], values[k])
        assert_frame_equal(filled, DataFrame(expected))

        result = df.replace([0, 2, 5], [5, 2, 0])
        expected = DataFrame({"A": [np.nan, 5, np.inf], "B": [5, 2, 0], "C": ["", "asdf", "fd"]})
        assert_frame_equal(result, expected)

        # dict to scalar
        filled = df.replace(to_rep, 0)
        expected = {}
        for k, v in compat.iteritems(df):
            expected[k] = v.replace(to_rep[k], 0)
        assert_frame_equal(filled, DataFrame(expected))

        self.assertRaises(TypeError, df.replace, to_rep, [np.nan, 0, ""])

        # scalar to dict
        values = {"A": 0, "B": -1, "C": "missing"}
        df = DataFrame({"A": [np.nan, 0, np.nan], "B": [0, 2, 5], "C": ["", "asdf", "fd"]})
        filled = df.replace(np.nan, values)
        expected = {}
        for k, v in compat.iteritems(df):
            expected[k] = v.replace(np.nan, values[k])
        assert_frame_equal(filled, DataFrame(expected))

        # list to list
        to_rep = [np.nan, 0, ""]
        values = [-2, -1, "missing"]
        result = df.replace(to_rep, values)
        expected = df.copy()
        for i in range(len(to_rep)):
            expected.replace(to_rep[i], values[i], inplace=True)
        assert_frame_equal(result, expected)

        self.assertRaises(ValueError, df.replace, to_rep, values[1:])

        # list to scalar
        to_rep = [np.nan, 0, ""]
        result = df.replace(to_rep, -1)
        expected = df.copy()
        for i in range(len(to_rep)):
            expected.replace(to_rep[i], -1, inplace=True)
        assert_frame_equal(result, expected)
Example #14
0
    def test_dropna_multiple_axes(self):
        df = DataFrame([[1, np.nan, 2, 3], [4, np.nan, 5, 6], [np.nan, np.nan, np.nan, np.nan], [7, np.nan, 8, 9]])
        cp = df.copy()
        result = df.dropna(how="all", axis=[0, 1])
        result2 = df.dropna(how="all", axis=(0, 1))
        expected = df.dropna(how="all").dropna(how="all", axis=1)

        assert_frame_equal(result, expected)
        assert_frame_equal(result2, expected)
        assert_frame_equal(df, cp)

        inp = df.copy()
        inp.dropna(how="all", axis=(0, 1), inplace=True)
        assert_frame_equal(inp, expected)
Example #15
0
 def test_regex_replace_series_of_regexes(self):
     mix = {"a": lrange(4), "b": list("ab.."), "c": ["a", "b", nan, "d"]}
     df = DataFrame(mix)
     s1 = Series({"b": r"\s*\.\s*"})
     s2 = Series({"b": nan})
     res = df.replace(s1, s2, regex=True)
     res2 = df.copy()
     res2.replace(s1, s2, inplace=True, regex=True)
     res3 = df.copy()
     res3.replace(regex=s1, value=s2, inplace=True)
     expec = DataFrame({"a": mix["a"], "b": ["a", "b", nan, nan], "c": mix["c"]})
     assert_frame_equal(res, expec)
     assert_frame_equal(res2, expec)
     assert_frame_equal(res3, expec)
Example #16
0
    def test_regex_replace_dict_mixed(self):
        mix = {"a": lrange(4), "b": list("ab.."), "c": ["a", "b", nan, "d"]}
        dfmix = DataFrame(mix)

        # dicts
        # single dict {re1: v1}, search the whole frame
        # need test for this...

        # list of dicts {re1: v1, re2: v2, ..., re3: v3}, search the whole
        # frame
        res = dfmix.replace({"b": r"\s*\.\s*"}, {"b": nan}, regex=True)
        res2 = dfmix.copy()
        res2.replace({"b": r"\s*\.\s*"}, {"b": nan}, inplace=True, regex=True)
        expec = DataFrame({"a": mix["a"], "b": ["a", "b", nan, nan], "c": mix["c"]})
        assert_frame_equal(res, expec)
        assert_frame_equal(res2, expec)

        # list of dicts {re1: re11, re2: re12, ..., reN: re1N}, search the
        # whole frame
        res = dfmix.replace({"b": r"\s*(\.)\s*"}, {"b": r"\1ty"}, regex=True)
        res2 = dfmix.copy()
        res2.replace({"b": r"\s*(\.)\s*"}, {"b": r"\1ty"}, inplace=True, regex=True)
        expec = DataFrame({"a": mix["a"], "b": ["a", "b", ".ty", ".ty"], "c": mix["c"]})
        assert_frame_equal(res, expec)
        assert_frame_equal(res2, expec)

        res = dfmix.replace(regex={"b": r"\s*(\.)\s*"}, value={"b": r"\1ty"})
        res2 = dfmix.copy()
        res2.replace(regex={"b": r"\s*(\.)\s*"}, value={"b": r"\1ty"}, inplace=True)
        expec = DataFrame({"a": mix["a"], "b": ["a", "b", ".ty", ".ty"], "c": mix["c"]})
        assert_frame_equal(res, expec)
        assert_frame_equal(res2, expec)

        # scalar -> dict
        # to_replace regex, {value: value}
        expec = DataFrame({"a": mix["a"], "b": [nan, "b", ".", "."], "c": mix["c"]})
        res = dfmix.replace("a", {"b": nan}, regex=True)
        res2 = dfmix.copy()
        res2.replace("a", {"b": nan}, regex=True, inplace=True)
        assert_frame_equal(res, expec)
        assert_frame_equal(res2, expec)

        res = dfmix.replace("a", {"b": nan}, regex=True)
        res2 = dfmix.copy()
        res2.replace(regex="a", value={"b": nan}, inplace=True)
        expec = DataFrame({"a": mix["a"], "b": [nan, "b", ".", "."], "c": mix["c"]})
        assert_frame_equal(res, expec)
        assert_frame_equal(res2, expec)
Example #17
0
    def test_frame_mixedtype_orient(self):  # GH10289
        vals = [
            [10, 1, "foo", 0.1, 0.01],
            [20, 2, "bar", 0.2, 0.02],
            [30, 3, "baz", 0.3, 0.03],
            [40, 4, "qux", 0.4, 0.04],
        ]

        df = DataFrame(vals, index=list("abcd"), columns=["1st", "2nd", "3rd", "4th", "5th"])

        self.assertTrue(df._is_mixed_type)
        right = df.copy()

        for orient in ["split", "index", "columns"]:
            inp = df.to_json(orient=orient)
            left = read_json(inp, orient=orient, convert_axes=False)
            assert_frame_equal(left, right)

        right.index = np.arange(len(df))
        inp = df.to_json(orient="records")
        left = read_json(inp, orient="records", convert_axes=False)
        assert_frame_equal(left, right)

        right.columns = np.arange(df.shape[1])
        inp = df.to_json(orient="values")
        left = read_json(inp, orient="values", convert_axes=False)
        assert_frame_equal(left, right)
Example #18
0
    def test_applymap(self):
        applied = self.frame.applymap(lambda x: x * 2)
        assert_frame_equal(applied, self.frame * 2)
        result = self.frame.applymap(type)

        # GH #465, function returning tuples
        result = self.frame.applymap(lambda x: (x, x))
        tm.assertIsInstance(result["A"][0], tuple)

        # GH 2909, object conversion to float in constructor?
        df = DataFrame(data=[1, "a"])
        result = df.applymap(lambda x: x)
        self.assertEqual(result.dtypes[0], object)

        df = DataFrame(data=[1.0, "a"])
        result = df.applymap(lambda x: x)
        self.assertEqual(result.dtypes[0], object)

        # GH2786
        df = DataFrame(np.random.random((3, 4)))
        df2 = df.copy()
        cols = ["a", "a", "a", "a"]
        df.columns = cols

        expected = df2.applymap(str)
        expected.columns = cols
        result = df.applymap(str)
        assert_frame_equal(result, expected)

        # datetime/timedelta
        df["datetime"] = Timestamp("20130101")
        df["timedelta"] = pd.Timedelta("1 min")
        result = df.applymap(str)
        for f in ["datetime", "timedelta"]:
            self.assertEqual(result.loc[0, f], str(df.loc[0, f]))
Example #19
0
    def test_index_with_nan(self):
        #  GH 2850
        df = DataFrame(
            {
                "id1": {0: "1a3", 1: "9h4"},
                "id2": {0: np.nan, 1: "d67"},
                "id3": {0: "78d", 1: "79d"},
                "value": {0: 123, 1: 64},
            }
        )

        # multi-index
        y = df.set_index(["id1", "id2", "id3"])
        result = y.to_string()
        expected = u"             value\nid1 id2 id3       \n1a3 NaN 78d    123\n9h4 d67 79d     64"
        self.assert_(result == expected)

        # index
        y = df.set_index("id2")
        result = y.to_string()
        expected = u"     id1  id3  value\nid2                 \nNaN  1a3  78d    123\nd67  9h4  79d     64"
        self.assert_(result == expected)

        # all-nan in mi
        df2 = df.copy()
        df2.ix[:, "id2"] = np.nan
        y = df2.set_index("id2")
        result = y.to_string()
        expected = u"     id1  id3  value\nid2                 \nNaN  1a3  78d    123\nNaN  9h4  79d     64"
        self.assert_(result == expected)
 def test_reject_missing_value(self):
     table = DataFrame([("a", 1, 2, 3)], columns=DEMAND_POINT_TABLE_COLUMNS)
     for column in DEMAND_POINT_TABLE_COLUMNS:
         t = table.copy()
         t[column][0] = float("nan")
         with raises(ValidationError):
             normalize_demand_point_table(t)
Example #21
0
    def testDataFrameNested(self):
        df = DataFrame([[1, 2, 3], [4, 5, 6]], index=["a", "b"], columns=["x", "y", "z"])

        nested = {"df1": df, "df2": df.copy()}

        exp = {"df1": ujson.decode(ujson.encode(df)), "df2": ujson.decode(ujson.encode(df))}
        self.assertTrue(ujson.decode(ujson.encode(nested)) == exp)

        exp = {
            "df1": ujson.decode(ujson.encode(df, orient="index")),
            "df2": ujson.decode(ujson.encode(df, orient="index")),
        }
        self.assertTrue(ujson.decode(ujson.encode(nested, orient="index")) == exp)

        exp = {
            "df1": ujson.decode(ujson.encode(df, orient="records")),
            "df2": ujson.decode(ujson.encode(df, orient="records")),
        }
        self.assertTrue(ujson.decode(ujson.encode(nested, orient="records")) == exp)

        exp = {
            "df1": ujson.decode(ujson.encode(df, orient="values")),
            "df2": ujson.decode(ujson.encode(df, orient="values")),
        }
        self.assertTrue(ujson.decode(ujson.encode(nested, orient="values")) == exp)

        exp = {
            "df1": ujson.decode(ujson.encode(df, orient="split")),
            "df2": ujson.decode(ujson.encode(df, orient="split")),
        }
        self.assertTrue(ujson.decode(ujson.encode(nested, orient="split")) == exp)
Example #22
0
 def test_blank_frame_no_memory(self):
     # One 1D stepper
     N = 5
     f = DataFrame({"x": np.arange(N), "y": np.ones(N), "frame": [0, 1, 2, 4, 5], "particle": [0, 0, 0, 1, 1]})
     expected = f.copy()
     actual = self.link_df(f, 5)
     assert_traj_equal(actual, expected)
Example #23
0
    def test_mixed_depth_pop(self):
        arrays = [
            ["a", "top", "top", "routine1", "routine1", "routine2"],
            ["", "OD", "OD", "result1", "result2", "result1"],
            ["", "wx", "wy", "", "", ""],
        ]

        tuples = zip(*arrays)
        tuples.sort()
        index = MultiIndex.from_tuples(tuples)
        df = DataFrame(randn(4, 6), columns=index)

        df1 = df.copy()
        df2 = df.copy()
        result = df1.pop("a")
        expected = df2.pop(("a", "", ""))
        assert_series_equal(expected, result)
        assert_frame_equal(df1, df2)
        self.assertEquals(result.name, "a")

        expected = df1["top"]
        df1 = df1.drop(["top"], axis=1)
        result = df2.pop("top")
        assert_frame_equal(expected, result)
        assert_frame_equal(df1, df2)
Example #24
0
def create(df: pd.DataFrame, values, x_values=None):
    """

    :param df:
    :param values:
    :param x_values:
    :return:
    """

    traces = []

    df = df.copy()  # type: pd.DataFrame

    if isinstance(values, str):
        df["y"] = df[values].tolist()
    else:
        try:
            df["y"] = values.tolist()
        except Exception:
            df["y"] = values

    if x_values is None:
        df["x"] = df.coupling_length.tolist()
    elif isinstance(x_values, str):
        df["x"] = df[x_values].tolist()
    else:
        try:
            df["x"] = x_values.tolist()
        except Exception:
            df["x"] = x_values

    for gait_id in df.gait_id.unique():
        df_slice = df[df.gait_id == gait_id]

        for index, row in df_slice.iterrows():
            traces.append(
                go.Scatter(
                    x=[row.x, row.x],
                    y=[0, row.y],
                    mode="lines",
                    hoverinfo="none",
                    line=dict(color=row.color, width=2),
                    legendgroup=gait_id,
                    showlegend=False,
                )
            )

        traces.append(
            go.Scatter(
                x=df_slice.x,
                y=df_slice.y,
                text=df_slice.short_id,
                name=gait_id,
                mode="markers",
                marker=dict(color=df_slice.color, size=10),
                legendgroup=gait_id,
            )
        )

    return traces
Example #25
0
    def test_replace_mixed(self):
        self.mixed_frame.ix[5:20, "foo"] = nan
        self.mixed_frame.ix[-10:, "A"] = nan

        result = self.mixed_frame.replace(np.nan, -18)
        expected = self.mixed_frame.fillna(value=-18)
        assert_frame_equal(result, expected)
        assert_frame_equal(result.replace(-18, nan), self.mixed_frame)

        result = self.mixed_frame.replace(np.nan, -1e8)
        expected = self.mixed_frame.fillna(value=-1e8)
        assert_frame_equal(result, expected)
        assert_frame_equal(result.replace(-1e8, nan), self.mixed_frame)

        # int block upcasting
        df = DataFrame({"A": Series([1.0, 2.0], dtype="float64"), "B": Series([0, 1], dtype="int64")})
        expected = DataFrame({"A": Series([1.0, 2.0], dtype="float64"), "B": Series([0.5, 1], dtype="float64")})
        result = df.replace(0, 0.5)
        assert_frame_equal(result, expected)

        df.replace(0, 0.5, inplace=True)
        assert_frame_equal(df, expected)

        # int block splitting
        df = DataFrame(
            {
                "A": Series([1.0, 2.0], dtype="float64"),
                "B": Series([0, 1], dtype="int64"),
                "C": Series([1, 2], dtype="int64"),
            }
        )
        expected = DataFrame(
            {
                "A": Series([1.0, 2.0], dtype="float64"),
                "B": Series([0.5, 1], dtype="float64"),
                "C": Series([1, 2], dtype="int64"),
            }
        )
        result = df.replace(0, 0.5)
        assert_frame_equal(result, expected)

        # to object block upcasting
        df = DataFrame({"A": Series([1.0, 2.0], dtype="float64"), "B": Series([0, 1], dtype="int64")})
        expected = DataFrame({"A": Series([1, "foo"], dtype="object"), "B": Series([0, 1], dtype="int64")})
        result = df.replace(2, "foo")
        assert_frame_equal(result, expected)

        expected = DataFrame({"A": Series(["foo", "bar"], dtype="object"), "B": Series([0, "foo"], dtype="object")})
        result = df.replace([1, 2], ["foo", "bar"])
        assert_frame_equal(result, expected)

        # test case from
        df = DataFrame({"A": Series([3, 0], dtype="int64"), "B": Series([0, 3], dtype="int64")})
        result = df.replace(3, df.mean().to_dict())
        expected = df.copy().astype("float64")
        m = df.mean()
        expected.iloc[0, 0] = m[0]
        expected.iloc[1, 1] = m[1]
        assert_frame_equal(result, expected)
Example #26
0
class ConcatenateBase(tm.TestCase):

    _multiprocess_can_split_ = True

    def setUp(self):
        self.frame = DataFrame(tm.getSeriesData())
        self.mixed_frame = self.frame.copy()
        self.mixed_frame["foo"] = "bar"
Example #27
0
    def test_mixed_depth_insert(self):
        arrays = [
            ["a", "top", "top", "routine1", "routine1", "routine2"],
            ["", "OD", "OD", "result1", "result2", "result1"],
            ["", "wx", "wy", "", "", ""],
        ]

        tuples = zip(*arrays)
        tuples.sort()
        index = MultiIndex.from_tuples(tuples)
        df = DataFrame(randn(4, 6), columns=index)

        result = df.copy()
        expected = df.copy()
        result["b"] = [1, 2, 3, 4]
        expected["b", "", ""] = [1, 2, 3, 4]
        assert_frame_equal(result, expected)
Example #28
0
    def test_interleave_non_unique_cols(self):
        df = DataFrame([[Timestamp("20130101"), 3.5], [Timestamp("20130102"), 4.5]], columns=["x", "x"], index=[1, 2])

        df_unique = df.copy()
        df_unique.columns = ["x", "y"]
        self.assertEqual(df_unique.values.shape, df.values.shape)
        tm.assert_numpy_array_equal(df_unique.values[0], df.values[0])
        tm.assert_numpy_array_equal(df_unique.values[1], df.values[1])
Example #29
0
    def test_sort_inplace(self):
        frame = DataFrame(np.random.randn(4, 4), index=[1, 2, 3, 4], columns=["A", "B", "C", "D"])

        sorted_df = frame.copy()
        sorted_df.sort_values(by="A", inplace=True)
        expected = frame.sort_values(by="A")
        assert_frame_equal(sorted_df, expected)

        sorted_df = frame.copy()
        sorted_df.sort_values(by="A", ascending=False, inplace=True)
        expected = frame.sort_values(by="A", ascending=False)
        assert_frame_equal(sorted_df, expected)

        sorted_df = frame.copy()
        sorted_df.sort_values(by=["A", "B"], ascending=False, inplace=True)
        expected = frame.sort_values(by=["A", "B"], ascending=False)
        assert_frame_equal(sorted_df, expected)
Example #30
0
 def test_one_trivial_stepper(self):
     # One 1D stepper
     N = 5
     f = DataFrame({"x": np.arange(N), "y": np.ones(N), "frame": np.arange(N)})
     expected = f.copy()
     expected["particle"] = np.zeros(N)
     actual = self.link_df(f, 5)
     assert_traj_equal(actual, expected)