pymc-devs / pytensor

PyTensor allows you to define, optimize, and efficiently evaluate mathematical expressions involving multi-dimensional arrays.
https://pytensor.readthedocs.io
Other
340 stars 101 forks source link

Move pandas converters to pytensor #63

Closed ferrine closed 2 months ago

ferrine commented 1 year ago

Description

Does it make sense to move the converters into pytensor? It will be slightly longer import time, but user experience will be better

try:
    import pandas as pd
    # https://github.com/pymc-devs/pymc/blob/main/pymc/aesaraf.py#L152
    @_as_tensor_variable.register(pd.Series)
    @_as_tensor_variable.register(pd.DataFrame)
    def dataframe_to_tensor_variable(df: pd.DataFrame, *args, **kwargs) -> TensorVariable:
        return at.as_tensor_variable(df.to_numpy(), *args, **kwargs)
except ImportError:
    pass
ricardoV94 commented 10 months ago

Is there a way to do this lazily?

HangenYuu commented 2 months ago

@ricardoV94 This is already in https://github.com/pymc-devs/pymc/blob/main/pymc/pytensorf.py#L147. With the current code, what do you mean by doing this lazily?

ricardoV94 commented 2 months ago

@ricardoV94 This is already in https://github.com/pymc-devs/pymc/blob/main/pymc/pytensorf.py#L147. With the current code, what do you mean by doing this lazily?

That it shouldn't fail if pandas is not installed. Maybe it's actually better to leave this in PyMC, since there is no perfect mapping from pandas to pytensor objects