Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ak.to_output() #56

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/awkward_pandas/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ def is_dataframe(cls, data):
def _to_output(cls, data):
raise NotImplementedError

def to_output(self, data):
def to_output(self, data=None):
data = data if data is not None else self.array
return self._to_output(data)

def apply(self, fn: Callable):
Expand Down
3 changes: 2 additions & 1 deletion src/awkward_pandas/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ def _to_output(cls, data):
pd.arrays.ArrowExtensionArray(ak.to_arrow(data, extensionarray=False))
)

def to_output(self, data):
def to_output(self, data=None):
# override to apply index
data = data if data is not None else self.array
arr = pd.arrays.ArrowExtensionArray(ak.to_arrow(data, extensionarray=False))
if self._obj is not None and len(arr) == len(self._obj.index):
return pd.Series(arr, index=self._obj.index)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,11 @@ def test_ufunc():

assert (s.ak + s.ak).tolist() == [[2, 4, 6], [8, 10], [12]]
assert (s.ak + s).tolist() == [[2, 4, 6], [8, 10], [12]]


def test_to_autoarrow():
a = [[1, 2, 3], [4, 5], [6]]
s = pd.Series(a)
s2 = s.ak.to_output()
assert s2.tolist() == a
assert "pyarrow" in str(s2.dtype)
Loading