Cant add or create new column for my data csv

hello everyone, im newbie in julia. i need help for my problem. I just to try using PyCall in my code. this is my code:

using Pandas
using Dates
@pyimport re
py"""
import pandas as pd

df = pd.read_csv(" my path tweets.csv", index_col = None, encoding="ISO-8859-1")

"""
df_sample = df.sample(n=200, random_state=42)
df_sample["Date"] = pd.to_datetime(df_sample["Date"], format="%a %b %d %H:%M:%S PDT %Y")
df_sample["Month"] = df_sample.Date.dt.month
df_sample["Month"].sort_values()

And i got warning this : sys:1: UserWarning: Pandas doesn’t allow columns to be created via a new attribute name.

myenv for python in julia is i use python 3.11.7.
hopefully all of u can help me, thanks.

Hi, and welcome to the Julia community!

The problem seems to be that df_sample["Date"] = ... (i.e. setindex!(df_sample, ..., "Date") gets converted to df_sample.Date = ... (setproperty!(df_sample, :Date, ...)), which is not allowed in Pandas:

function setindex!(o::PyObject, v, s::Union{Symbol, AbstractString})
    Base.depwarn("`setindex!(o::PyObject, v, s::Union{Symbol, AbstractString})` is deprecated in favor of `setproperty!(o, s, v)`.", :setindex!)
    return _setproperty!(o, s, v)
end

(you can find this by prepending @which in front of the df_sample["Data"] = ... line.)

As a workaround you could use e.g. df_sample = df_sample.assign(Date = ...) In general, I’ll refer to the Pandas documentation for alternative ways to add columns.


By the way, note that PythonCall.jl does not exhibit this issue:

using Pkg
Pkg.add("PythonCall")

ENV["PYTHON"] = "/path/to/your/python/envs/executable"  
using PythonCall  # after setting the environment variable

pd = pyimport("pandas")
df = pd.read_csv("data.csv")
#=
Python:
     A    B    C
0  1.0  2.0  3.0
1 -1.0 -2.0 -3.0
=#
df["New"] = ["x", "y"]
display(df)
#=
Python:
     A    B    C New
0  1.0  2.0  3.0   x
1 -1.0 -2.0 -3.0   y
=#