JuliaPy / JuliaPy/PythonCall.jl

Pandas compatibility

未关闭
#501 6 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
bug
主要语言
Julia
星标
1.1k
派生
86
平均合并
1 天 22 小时
30 天内合并 PR
3

描述

**Affects:** PythonCall

**Describe the bug**

I have been trying to use pandas from PythonCall.jl and just wanted to document a few different calls that do not directly translate to Julia. I guess this might just mean we need a `PythonPandas` package to translate calls but I wonder if there's any missing methods that could be implemented to fix things automatically.

First, the preamble for this:

```julia
using PythonCall

pd = pyimport("pandas")
```

- [ ] **1. Constructing `pandas.DataFrame`:**

Using a similar syntax to Python:

```julia
df = pd.DataFrame(Dict([
"a" => [1, 2, 3],
"b" => [4, 5, 6]
]))
```

which results in the following dataframe:

```julia
julia> df
Python:
0
0 b
1 a
```

i.e., it seems to have a single column named "0" and rows for a and b.

If I instead write this as a vector of pairs, I get:

```julia
julia> pd.DataFrame([
"a" => [1, 2, 3],
"b" => [4, 5, 6]
])
Python:
0 1
0 a [1, 2, 3]
1 b [4, 5, 6]
```

I suppose this one makes sense.

I was able to get it working with the following syntax instead:

```julia
julia> df = pd.DataFrame([
1 4
2 5
3 6
], columns=["a", "b"])
Python:
a b
0 1 4
1 2 5
2 3 6
```

- [ ] **2. Selecting multiple columns**

So, selecting a single column works:

```julia
julia> df["a"]
Python:
0 1
1 2
2 3
Name: a, dtype: int64
```

but multiple columns does not:

```julia
julia> df[["a", "b"]]
ERROR: Python: TypeError: Julia: MethodError: objects of type Vector{String} are not callable
Use square brackets [] for indexing an Array.
Python stacktrace:
[1] __call__
@ ~/.julia/packages/PythonCall/S5MOg/src/JlWrap/any.jl:223
[2] apply_if_callable
@ pandas.core.common ~/Documents/pysr_projects/arya/bigbench/.CondaPkg/env/lib/python3.12/site-packages/pandas/core/common.py:384
[3] __getitem__
@ pandas.core.frame ~/Documents/pysr_projects/arya/bigbench/.CondaPkg/env/lib/python3.12/site-packages/pandas/core/frame.py:4065
Stacktrace:
[1] pythrow()
@ PythonCall.Core ~/.julia/packages/PythonCall/S5MOg/src/Core/err.jl:92
[2] errcheck
@ ~/.julia/packages/PythonCall/S5MOg/src/Core/err.jl:10 [inlined]
[3] pygetitem(x::Py, k::Vector{String})
@ PythonCall.Core ~/.julia/packages/PythonCall/S5MOg/src/Core/builtins.jl:171
[4] getindex(x::Py, i::Vector{String})
@ PythonCall.Core ~/.julia/packages/PythonCall/S5MOg/src/Core/Py.jl:292
[5] top-level scope
@ REPL[18]:1
```

I got around this by inserting a `pylist` call:

```julia
julia> df[pylist(["a", "b"])]
Python:
a b
0 1 4
1 2 5
2 3 6
```

贡献指南

这个仓库没有索引到贡献指南

调研方向

Reproduce the DataFrame construction and multi-column selection examples with PythonCall and pandas. Then inspect src/JlWrap/any.jl, src/Core/builtins.jl, and src/Core/Py.jl, which appear in the reported stack traces. Done means the intended pandas calls either work directly or the supported Julia-side usage is clearly documented and covered by relevant tests.

由索引模型根据 Issue 内容生成。

评估

技术栈
julia, pandas, python
领域
api, data
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
需要澄清
新手友好度
32/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。