1

I have defined a pandas DataFrame as follows:

df_tmp = pd.DataFrame({'EDT': pd.Series(dtype='datetime64[ns]'),
                       'FSPB': pd.Series(dtype='str'),
                       'FS_LA': pd.Series(dtype='str'),
                       'lA': pd.Series(dtype='int'),
                       'avg': pd.Series(dtype='float64'),
                       'nw': pd.Series(dtype='float64')})

Is there any way to convert the above into an empty polars DataFrame?

2 Answers 2

3

According to the polars docs, polars DataFrames can take a pandas DataFrame in their constructor, so:

import pandas as pd
import polars as pl

df_tmp = pd.DataFrame({'EDT': pd.Series(dtype='datetime64[ns]'),
                       'FSPB': pd.Series(dtype='str'),
                       'FS_LA': pd.Series(dtype='str'),
                       'lA': pd.Series(dtype='int'),
                       'avg': pd.Series(dtype='float64'),
                       'nw': pd.Series(dtype='float64')})

df = pl.DataFrame(df_tmp)

should work.

Sign up to request clarification or add additional context in comments.

4 Comments

tried exactly this but it does not work. It somewhat looks like this cannot be created without data. But I need an EMPTY pl.DataFrame.
Interestingly enough, it seems to work for me. Running my code results in an empty polars dataframe (shaped 0,6) with the correct dtypes in each column. It's possible you're missing pyarrow, which is required for this? In any case, either my or ritchie's method above should both function, so try his answer as well.
Yeap.. your function should work as well. OP might need to update polars and install pyarrow.
thank you all! A clean venv with reinstallation of polars and pyarrow did the job!
1
import polars as pl
import pandas as pd

pandas_df = pd.DataFrame({'EDT': pd.Series(dtype='datetime64[ns]'),
                       'FSPB': pd.Series(dtype='str'),
                       'FS_LA': pd.Series(dtype='str'),
                       'lA': pd.Series(dtype='int'),
                       'avg': pd.Series(dtype='float64'),
                       'nw': pd.Series(dtype='float64')})

pl.from_pandas(pandas_df)
shape: (0, 6)
┌──────────────┬──────┬───────┬─────┬─────┬─────┐
│ EDT          ┆ FSPB ┆ FS_LA ┆ lA  ┆ avg ┆ nw  │
│ ---          ┆ ---  ┆ ---   ┆ --- ┆ --- ┆ --- │
│ datetime[ns] ┆ str  ┆ str   ┆ i64 ┆ f64 ┆ f64 │
╞══════════════╪══════╪═══════╪═════╪═════╪═════╡
└──────────────┴──────┴───────┴─────┴─────┴─────┘

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.