Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

docs: update struct examples. #953

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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 4, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ def field(self, name_or_index: str | int):
>>> bpd.options.display.progress_bar = None
>>> s = bpd.Series(
... [
... {"version": 1, "project": "pandas"},
... {"version": 2, "project": "pandas"},
... {"version": 1, "project": "numpy"},
... {"project": "pandas", "version": 1},
... {"project": "pandas", "version": 2},
... {"project": "numpy", "version": 1},
... ],
... dtype=bpd.ArrowDtype(pa.struct(
... [("version", pa.int64()), ("project", pa.string())]
... [("project", pa.string()), ("version", pa.int64())]
... ))
... )

Expand All @@ -41,7 +41,7 @@ def field(self, name_or_index: str | int):

Extract by field index.

>>> s.struct.field(0)
>>> s.struct.field(1)
0 1
1 2
2 1
Expand All @@ -68,22 +68,22 @@ def explode(self):
>>> bpd.options.display.progress_bar = None
>>> s = bpd.Series(
... [
... {"version": 1, "project": "pandas"},
... {"version": 2, "project": "pandas"},
... {"version": 1, "project": "numpy"},
... {"project": "pandas", "version": 1},
... {"project": "pandas", "version": 2},
... {"project": "numpy", "version": 1},
... ],
... dtype=bpd.ArrowDtype(pa.struct(
... [("version", pa.int64()), ("project", pa.string())]
... [("project", pa.string()), ("version", pa.int64())]
... ))
... )

Extract all child fields.

>>> s.struct.explode()
version project
0 1 pandas
1 2 pandas
2 1 numpy
project version
0 pandas 1
1 pandas 2
2 numpy 1
<BLANKLINE>
[3 rows x 2 columns]

Expand Down Expand Up @@ -113,8 +113,8 @@ def dtypes(self):
... ))
... )
>>> s.struct.dtypes()
version Int64
project string[pyarrow]
version Int64
dtype: object

Returns:
Expand All @@ -140,21 +140,21 @@ def explode(self, column, *, separator: str = "."):
>>> countries = bpd.Series(["cn", "es", "us"])
>>> files = bpd.Series(
... [
... {"version": 1, "project": "pandas"},
... {"version": 2, "project": "pandas"},
... {"version": 1, "project": "numpy"},
... {"project": "pandas", "version": 1},
... {"project": "pandas", "version": 2},
... {"project": "numpy", "version": 1},
... ],
... dtype=bpd.ArrowDtype(pa.struct(
... [("version", pa.int64()), ("project", pa.string())]
... [("project", pa.string()), ("version", pa.int64())]
... ))
... )
>>> downloads = bpd.Series([100, 200, 300])
>>> df = bpd.DataFrame({"country": countries, "file": files, "download_count": downloads})
>>> df.struct.explode("file")
country file.version file.project download_count
0 cn 1 pandas 100
1 es 2 pandas 200
2 us 1 numpy 300
country file.project file.version download_count
0 cn pandas 1 100
1 es pandas 2 200
2 us numpy 1 300
<BLANKLINE>
[3 rows x 4 columns]

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.