Skip to content

Commit

Permalink
Merge branch 'main' into chore/asv
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Sep 13, 2023
2 parents 878531c + e097bd9 commit 21e78f0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
20 changes: 20 additions & 0 deletions python/xorbits/_mars/dataframe/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,16 @@ def __str__(self):
def __repr__(self):
return self._to_str(representation=True)

def _to_arr(self):
if len(self._executed_sessions) == 0: # pragma: no cover
raise NotImplementedError

data = self.fetch(session=self._executed_sessions[-1])
return np.asarray(data)

def __array__(self):
return self._to_arr()

def _to_mars_tensor(self, dtype=None, order="K", extract_multi_index=False):
tensor = self.to_tensor(extract_multi_index=extract_multi_index)
dtype = dtype if dtype is not None else tensor.dtype
Expand Down Expand Up @@ -1414,6 +1424,16 @@ def __str__(self):
def __repr__(self):
return self._to_str(representation=False)

def _to_arr(self):
if len(self._executed_sessions) == 0: # pragma: no cover
raise NotImplementedError

data = self.fetch(session=self._executed_sessions[-1])
return np.asarray(data)

def __array__(self):
return self._to_arr()

@property
def dtype(self):
return getattr(self, "_dtype", None) or getattr(self.op, "dtype", None)
Expand Down
24 changes: 24 additions & 0 deletions python/xorbits/_mars/dataframe/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,27 @@ def test_mars_tensor_magic(setup):
np.testing.assert_array_equal(expected, actual)
with pytest.raises(ValueError, match="could not convert string to float"):
DataFrame(expected).__mars_tensor__(dtype="float64").execute()


def test_series_and_index_array(setup):
data = np.random.rand(10)
series = Series(data).execute()

array = np.array(series)
np.testing.assert_array_equal(array, data)

df = pd.DataFrame({"a": [1, 2], "b": ["foo", "bar"]})
xdf = DataFrame(df)
index = xdf.index.execute()
np.testing.assert_array_equal(np.array(df.index), np.array(index))

arrays = [
["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"],
["one", "two", "one", "two", "one", "two", "one", "two"],
]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"])
s = pd.Series(np.random.randn(8), index=index)
xs = Series(s).index.execute()

np.testing.assert_array_equal(np.array(s.index), np.array(xs))

0 comments on commit 21e78f0

Please sign in to comment.