News for hackers. Stuff that matters.
Re:Modern Pandas (Part 2): Method Chaining by account-5 on Sunday May 01, 2022 @10:41AM (#31225383)
I've always found pandas really hard to use or reason about. I eventually get there but I don't like the code. Obviously subjective. I've never used another "data science" language though so I've no experience beyond it.
Re:Modern Pandas (Part 2): Method Chaining by tomtom1337 on Sunday May 01, 2022 @11:23AM (#31225757)
I'd recommend checking out polars as an alternative to pandas - https://github.com/pola-rs/polars

It has a rather different api, and is significantly faster. Highly recommend it.

Re:Modern Pandas (Part 2): Method Chaining by riyadparvez on Sunday May 01, 2022 @11:51AM (#31226022)
The issue is there's a huge ecosystem developed on top of Pandas. So if it's different API, now you lose access to all the ecosystem.
Re:Modern Pandas (Part 2): Method Chaining by ritchie46 on Sunday May 01, 2022 @01:14PM (#31226706)
Should not be a real blocker. There is good interop with arrow, numpy and pandas. Where arrow and numpy mostly is zero-copy.

So you are one `df.to_numpy()/df.to_pandas()` away to `X` libary you want to use.

Re:Modern Pandas (Part 2): Method Chaining by nojito on Sunday May 01, 2022 @12:53PM (#31226531)
Polars can export to arrow which you can then load into pandas.

Or you can convert it directly into a pandas Df

Re:Modern Pandas (Part 2): Method Chaining by smartmic on Sunday May 01, 2022 @04:38PM (#31228440)
If you are ready to stretch your mind and have time and energy to learn a new skill, I recommend this as "data science" language: APL

Background: I first gained some experience with J, where I first learned to appreciate the advantages of array languages. The main advantage is actually having your own way of thinking about processing multidimensional data. Now, recently, I've gotten into the notation of APL, and it's really even cooler than the ASCII "noise" of J. The symbols make it easier for me to both write and read programs. Admittedly, for more complex operations with data it takes a lot of learning that you usually don't have. But for simple transformation APL is already quite fast usable and convinces beyond the "mainstream".

Re:Modern Pandas (Part 2): Method Chaining by account-5 on Sunday May 01, 2022 @05:36PM (#31228924)
APL and J are on my list of programming languages I'd like to learn. I've been listening to the Array Cast podcast for a while. I just need a place to start. I don't really learn a language until I use it for something, at the moment I have nothing to use APL for. Any recommendations of where to start would be welcome.
Re:Modern Pandas (Part 2): Method Chaining by mjburgess on Sunday May 01, 2022 @11:13AM (#31225674)
Everyone does. Perhaps if python would add more support for FP (rather than its present hostility), we'd be able to phrase data transformations more naturally in the language.
Re:Modern Pandas (Part 2): Method Chaining by zasdffaa on Sunday May 01, 2022 @12:15PM (#31226221)
My python is rusty but IIRC it allows functional stuff (except for expression-only lambdas, boo).

From https://docs.python.org/3/howto/functional.html it's got map/filter/currying and plenty more, what's misting in your view?

Re:Modern Pandas (Part 2): Method Chaining by mjburgess on Sunday May 01, 2022 @12:24PM (#31226300)
pattern matching expressions, syntax for partial application and composition, a typing system which can express structural types, a generalised list comprehension
Re:Modern Pandas (Part 2): Method Chaining by zasdffaa on Sunday May 01, 2022 @12:32PM (#31226363)
> pattern matching expressions,

nothing intrinsically 'functional' about this, though it's nice

> syntax for partial application

<https://docs.python.org/3/library/functools.html#functools.p...>

> and composition,

Hmm, to my surprise I can't find this but TBH it's trivial to write, here's an eg <https://stackoverflow.com/questions/16739290/composing-funct...>

> a typing system which can express structural types,

typing is orthogonal to FP (though very nice to have)

> a generalised list comprehension

IDK what this means, what's ungeneral about comprehensions currently?

Re:Modern Pandas (Part 2): Method Chaining by mjburgess on Sunday May 01, 2022 @01:21PM (#31226780)
expressing generic data transformations naturally in syntax, requires: being able to wrap/unwrap data types (ie., pattern matching), composing operations, partially applying them, and providing syntax to support filter/map/flatMap on arbitrary data structures

Pythons failure to provide this, indeed, outright hostility to doing so is half the reason pandas is a mess of incomprehensible syntax

By prioritising assignment expressions and implementing pattern "matching" as a statement, they're clearly showing a lot of hostility to one of the major use cases of their language

(incidentally, recall that C# introduced LINQ in 2008, a generalised comprehension! That's C#.)

There isnt the syntax to reimplement to support natural phrasings of data transformation, in lieu of this, pandas exploits weird operators such as `.loc[a,b]`.

At some point the dam is going to have to break, and python is going to have to introduce something to resolve this mess. However, i'd bet it'll be a decade of tooth-and-nail fighting about it. It isnt a software engineering language for education any more, and i'm not seeing this reality being acknowledged

Re:Modern Pandas (Part 2): Method Chaining by ok_dad on Sunday May 01, 2022 @04:45PM (#31228511)
> pattern matching expressions

Available in 3.10: https://peps.python.org/pep-0636/

> syntax for partial application...

    from functools import partial

    somefunc_arg1_arg2 = partial(somefunc, arg1, arg2)
> ...and composition

A native compositional syntax would be nice.

> a typing system which can express structural types

    # mypy will typecheck this code and see `MyString()` has a `.read()` method, so it's a `Readable` even though it doesn't sub-class `Readable`

    from typing import Any, Protocol

    class Readable(Protocol):
        def read(self) -> Any: ...

    def read_something(something: Readable) -> Any:
        return something.read()

    class MyString:
        a_string: str = "something"
        def read(self) -> str:
            return self.a_string

    read_something(MyString())
> a generalised list comprehension

I agree this would be nice.

Re:Modern Pandas (Part 2): Method Chaining by mjburgess on Monday May 02, 2022 @06:12AM (#31233215)
they arent expressions, defeating the whole point

that isnt syntax for partial appication, and the protocol system for structrural typing is syntactically and practically absurd

the question is "why do data processing libs in python look syntactically illegible" and the answer is largely, as youve posted above, that python doesnt support useful syntax

Re:Modern Pandas (Part 2): Method Chaining by biellls on Sunday May 01, 2022 @02:27PM (#31227423)
Pattern matching is supported in 3.10 and can match classes structurally, as well as other types (edit: I see that you mean you don't like that it's a statement, which I agree with). The typing system supports structural types with Protocol. Personally what I miss most are multi-line lambdas.
Re:Modern Pandas (Part 2): Method Chaining by mjburgess on Monday May 02, 2022 @06:16AM (#31233240)
I'm not asking for "support" i'm asking for syntax. The question is why the syntax of data processing in python is illegible, not "are things technically possible in python".

Commenters here are keen to say "but dont you know!" -- and, yes, I do.

Re:Modern Pandas (Part 2): Method Chaining by zasdffaa on Monday May 02, 2022 @06:41AM (#31233439)
To be fair the confusion of FP vs FP syntax is from your comment: "Perhaps if python would add more support for FP (rather than its present hostility)"

Python does FP fine and FP via an ugly library or via elegant inbuilt syntax is still FP. I get you want nicer syntax but it wasn't clear.

I dunno, write a PEP and see if it gets support. I hope you succeed!

Re:Modern Pandas (Part 2): Method Chaining by animal_spirits on Sunday May 01, 2022 @11:45AM (#31225966)
Pandas makes it really hard to do easy things, and really easy to do hard things. But its more useful to the people who need it to do the hard things.
Re:Modern Pandas (Part 2): Method Chaining by otsaloma on Sunday May 01, 2022 @02:43PM (#31227543)
Here's another alternative. I wrote Dataiter specifically as I too was frustrated with Pandas. In my experience if you design a new API from scratch (and don't try to reimplement the Pandas API as many projects have done!) and have some vision and consistent principles, it's well possible to get a good intuitive API as a result. Two relevant issues remain: You're limited by NumPy's datatypes and their problems, such as memory-hogging strings and a lack of a proper missing value (NA), and secondly, limited by the Python language, so compared to e.g. dplyr's non-standard evaluation, you'll need to use lambda functions, which are unfortunately clumsy and verbose.

https://github.com/otsaloma/dataiter

Here's a comparison of dplyr vs. Dataiter vs. Pandas, which should give quick overview of the similarieties and differences.

https://dataiter.readthedocs.io/en/latest/_static/comparison...

Re:Modern Pandas (Part 2): Method Chaining by biellls on Sunday May 01, 2022 @02:23PM (#31227385)
A lot of people hate on SQL. I used to be one of them but I've come to think that for data transformations it's hard to beat. My current favorite is DuckDB, which is like a SQLite but columnar. It has great performance and it's easy to call it from python and even run SQL on pandas dataframes.