Code Monkey home page Code Monkey logo

Comments (5)

cjackal avatar cjackal commented on June 25, 2024 1

You can control the way polars interprete 2D data input with orient keyword:

x2 = [[1, 2], [3, 4]]
df1 = pl.DataFrame(x2, schema=['c0', 'c1'], orient="row")

Note that for a square matrix like yours both orientation are valid and make sense. Although the default orientation for polars (orient="col") here differs from that of pandas, which may confuse users from pandas.

from polars.

alexander-beedie avatar alexander-beedie commented on June 25, 2024 1

Yup, this is not a bug. Polars/Arrow are column-oriented by design, so when there is ambiguity (same number of rows/columns and the schema types don't help and you have not set the "orient" parameter), "col" will be the default.

This is detailed in the DataFrame docstring:

orient{‘col’, ‘row’}, default None
    Whether to interpret two-dimensional data as columns or as rows. If None, 
    the orientation is inferred by matching the columns and data dimensions.
    If this does not yield conclusive results, column orientation is used.

Note that, if you have the option, column data will load more efficiently; otherwise, set orient="row" explicitly to avoid the need for any data-level inference 👍

from polars.

cjackal avatar cjackal commented on June 25, 2024

@alexander-beedie Another (relevant) confusion as a polars newbie - when the 2D list is converted as numpy array, it's default orientation becomes "row", and this behavior change seems not documented:

x2 = [[1, 2], [3, 4]]
pl.DataFrame(x2, schema=['c0', 'c1'])
# shape: (2, 2)
# ┌─────┬─────┐
# │ c0  ┆ c1  │
# │ --- ┆ --- │
# │ i64 ┆ i64 │
# ╞═════╪═════╡
# │ 1   ┆ 3   │
# │ 2   ┆ 4   │
# └─────┴─────┘
pl.DataFrame(np.asarray(x2), schema=['c0', 'c1'])
# shape: (2, 2)
# ┌─────┬─────┐
# │ c0  ┆ c1  │
# │ --- ┆ --- │
# │ i64 ┆ i64 │
# ╞═════╪═════╡
# │ 1   ┆ 2   │
# │ 3   ┆ 4   │
# └─────┴─────┘

If I were to suggest, such a silent behavior change is often dangerous (as a full-time ML engineer, I spent way more time debugging a silent behavior change than fixing a noisy spam of warnings, silent killer is a true evil...), it would be consistent across input class or well-documented at least.

from polars.

zacmon avatar zacmon commented on June 25, 2024

Thank you both for clarifying! I'm leaving the issue open due to @cjackal's observation about the inconsistency. Once that is resolved, whoever would like can close the issue.

from polars.

stinodego avatar stinodego commented on June 25, 2024

People bump their heads on this one all the time. This must be the 5th issue with this exact complaint.

I think it's time to flip the switch and use row-orientation by default for sequence-of-sequences (if we cannot infer that it should be column-oriented). It just makes sense to parse these as rows - we have the dict format for column-oriented input. And we do the same for NumPy inputs.

@ritchie46 What do you think?

from polars.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.