Search code examples
pythonpython-polars

How to fix PanicException when converting int to pl.Date


I am trying to convert a 4 million pl.Series from i64 to pl.Date. My code does not work because of PanicException which nature I am not really familiar with:

sample = pl.DataFrame({
    'install_time': [1595404176, 
                     1595404176,
                     1595404177,
                     1595404180,
                     1595404184,
                     1595404185,
                     1595404191,
                     1595404192,
                     1595404195]
})

sample.with_columns(pl.col('install_time').cast(pl.Date))

As the result of the execution of the piece of code above I receive the following error: PanicException: out-of-range date. I really do not have idea on how to fix so I will be grateful for any advice and suggestions.


Solution

  • As was suggested in comments by @h4z3, I converted UNIX to datetime and then to date. It worked fine for me:

    sample.with_columns(pl.from_epoch('install_time', time_unit='s').cast(pl.Date).alias('dt_install'))