Open jr200 opened 1 year ago
This also applies to Enums, and to transpose()
:
>>> pl.DataFrame({'a': [1], 'b': ['foo']}).transpose().dtypes
[String]
>>> pl.DataFrame({'a': [1], 'b': pl.Series(['foo'], dtype=pl.Categorical)}).transpose().dtypes
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "polars/dataframe/frame.py", line 4033, in transpose
return self._from_pydf(self._df.transpose(keep_names_as, column_names))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
polars.exceptions.ComputeError: failed to determine supertype of i64 and cat
>>> pl.DataFrame({'a': [1], 'b': pl.Series(['foo'], dtype=pl.Enum(['foo']))}).transpose().dtypes
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "polars/dataframe/frame.py", line 4033, in transpose
return self._from_pydf(self._df.transpose(keep_names_as, column_names))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
polars.exceptions.ComputeError: failed to determine supertype of i64 and enum
At least in the case of enum
and cat
, we could say cat
is the supertype?
@s-banach seemingly, but the new cat wouldn't necessarily have the same encodings/StringCache (not sure what term is right here) as the original so could be problematic. Here's a bug actually:
df=pl.DataFrame({'a':pl.Series(['apple', 'banana', 'carrot'], dtype=pl.Enum(['apple', 'banana', 'carrot'])),
'b':pl.Series(['planes','trains','automobiles'], dtype=pl.Categorical)})
shape: (3, 2)
┌────────┬─────────────┐
│ a ┆ b │
│ --- ┆ --- │
│ enum ┆ cat │
╞════════╪═════════════╡
│ apple ┆ planes │
│ banana ┆ trains │
│ carrot ┆ automobiles │
└────────┴─────────────┘
df.with_columns(pl.col('a').cast(pl.Categorical)).melt()
shape: (6, 2)
┌──────────┬────────┐
│ variable ┆ value │
│ --- ┆ --- │
│ str ┆ cat │
╞══════════╪════════╡
│ a ┆ apple │
│ a ┆ banana │
│ a ┆ carrot │
│ b ┆ apple │
│ b ┆ banana │
│ b ┆ carrot │
└──────────┴────────┘
So it needs to remap categoricals when melted (or at least disallow and raise)
Checks
[X] I have checked that this issue has not already been reported.
[X] I have confirmed this bug exists on the latest version of Polars.
Reproducible example
Log output
Issue description
This might be expected behaviour, but I wanted to check.
Unable to perform a melt between:
Is it reasonable to say the super-type of these combinations are
str
? Orcat
even?Expected behavior
Output:
Installed versions