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

Passing int should broadcast to fill series in DataFrame #2812

Closed
thomasaarholt opened this issue Mar 2, 2022 · 2 comments
Closed

Passing int should broadcast to fill series in DataFrame #2812

thomasaarholt opened this issue Mar 2, 2022 · 2 comments

Comments

@thomasaarholt
Copy link
Contributor

thomasaarholt commented Mar 2, 2022

Describe your feature request

With numpy and pandas you can expect that setting arr[:] = some_int will broadcast the shape of some_int to fill the shape of the dimension being set.

With polars:

import polars as pl
a = {"foo":[1,2,3],}
df = pl.DataFrame(a)
df["foo"] = [1,2,3]
df["foo"] = 1 # <-------- Error is here

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
File ~/mambaforge/envs/geo2/lib/python3.9/site-packages/polars/internals/frame.py:1458, in DataFrame.__setitem__(self, key, value)
   1457 try:
-> 1458     self.replace(key, pli.Series(key, value))
   1459 except Exception:

File ~/mambaforge/envs/geo2/lib/python3.9/site-packages/polars/internals/series.py:227, in Series.__init__(self, name, values, dtype, strict, nan_to_null)
    226 else:
--> 227     raise ValueError("Series constructor not called properly.")

ValueError: Series constructor not called properly.

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
Input In [24], in <module>
      3 df = pl.DataFrame(a)
      4 df["foo"] = [1,2,3]
----> 5 df["foo"] = 1

File ~/mambaforge/envs/geo2/lib/python3.9/site-packages/polars/internals/frame.py:1460, in DataFrame.__setitem__(self, key, value)
   1458         self.replace(key, pli.Series(key, value))
   1459     except Exception:
-> 1460         self.hstack([pli.Series(key, value)], in_place=True)
   1461 # df[["C", "D"]]
   1462 elif isinstance(key, list):

File ~/mambaforge/envs/geo2/lib/python3.9/site-packages/polars/internals/series.py:227, in Series.__init__(self, name, values, dtype, strict, nan_to_null)
    225     self._s = pandas_to_pyseries(name, values)
    226 else:
--> 227     raise ValueError("Series constructor not called properly.")

ValueError: Series constructor not called properly.
@ritchie46
Copy link
Member

No it should not. Thanks for the reminder to deprecate DataFrame.__setitem__. ;)

Try this:

df = pl.DataFrame({
    "foo":[1,2,3]
})

df.with_column(pl.lit(1).alias("bar"))

@thomasaarholt
Copy link
Contributor Author

Hahaha, very well :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants