Latest / Talk Python To Me / #510: 10 Polars Tools and Techniques To Level Up Your Data Science
Transcript
- 0:00Are you using Polars for your data science work?
- 0:02There are many benefits to Polars directly, of course,
- 0:05but you might not be aware of all the excellent tools and libraries that make Polars even better.
- 0:11Examples include Patito, which combines Pydantic and Polars for data validation,
- 0:17and Polars underscore encryption, which adds AES encryption to selected columns.
- 0:23We have Christopher Trudeau back on Talk Python To Me to tell us all about his list of excellent libraries
- 0:30to power up your Polars game, and even talk a bit about his new Polars course.
- 0:36This is Talk Python To Me, episode 510, recorded May 22nd, 2025.
- 0:58Check it out, folks.
- 0:59we've got a new geeky theme song. I hope you enjoy this fun start to the episode.
- 1:04If you want to download the entire mp3, grab it at talkpython.fm/flask song. Enjoy.
- 1:12Welcome to Talk Python To Me, a weekly podcast on Python. This is your host, Michael Kennedy.
- 1:18Follow me on Mastodon where I'm @mkennedy and follow the podcast using at talkpython,
- 1:23both accounts over at fosstodon.org and keep up with the show and listen to over nine years of
- 1:29at talkpython.fm. If you want to be part of our live episodes, you can find the live streams over
- 1:35on YouTube. Subscribe to our YouTube channel over at talkpython.fm/youtube and get notified
- 1:40about upcoming shows. This episode is brought to you by Sentry. Don't let those errors go unnoticed.
- 1:46Use Sentry like we do here at Talk Python. Sign up at talkpython.fm/sentry. And it's brought
- 1:52to you by Agency. Discover agentic AI with Agency. Their layer lets agents find, connect, and work
- 1:59together any stack anywhere start building the internet of agents at talkpython.fm/agency
- 2:05spelled a-g-n-t-c-y christopher welcome to talk python welcome back to talk python thank you
- 2:12third time's the charm we'll see how it goes it's gonna go well i'm sure um we've got a really fun
- 2:18topic lined up i think this is one of those kinds of shows where there's there's going to be something
- 2:24for everybody. There might be a lot for some people and there's going to be like, oh, that's
- 2:29the thing that made it worth listening because we're going to talk about a bunch of different
- 2:33extensions, tools, techniques, et cetera, for working with Polars, one of the exciting data
- 2:38frame libraries that we have these days. So super excited to have you here to tell us about that.
- 2:44And I'm really interested to hear how it works with Django because that's what you talk about
- 2:48and coming, right?
- 2:50I honestly, I haven't done it.
- 2:53There's no reason it shouldn't,
- 2:55but I have yet to actually combine
- 2:57the two pieces together.
- 2:59Well, I have used Flask and Pandas together,
- 3:03so surely Django and Polaris will go together.
- 3:05I know actually that they would.
- 3:07Shouldn't be a problem.
- 3:08I think it's one of the beauties
- 3:10of these kinds of libraries is at the heart,
- 3:13although there are rust underneath
- 3:14or other things underneath to get you the performance,
- 3:17they're still just Python.
- 3:20So, you know, you've got access to the code
- 3:21and you can do what you want.
- 3:23Yeah, I think there's probably some combinations
- 3:25that people don't typically think of.
- 3:27Like, for example, one of the things that I like to do
- 3:30is I've got some dashboard-y thing,
- 3:34either for Talk Python in the course, I can't remember,
- 3:36but I want a bar graph.
- 3:37And so I do some matplotlib thing
- 3:40and have it generate a picture
- 3:41and just return that as a picture dynamically at a URL,
- 3:45you know, and just set the source.
- 3:47to the image to be wherever that thing is generated from.
- 3:50So there are really cool combinations,
- 3:52though I suspect we will not be doing much of our typical web stuff this show, will we?
- 3:57I'll drop mentions to my book because I have to.
- 4:01That's the way it's supposed to work.
- 4:02But otherwise, yeah, we'll stick to the data science world this time around.
- 4:06Yeah, well, let's start there.
- 4:07I know people probably have listened to you on the show
- 4:10or on the RealPython podcast or in other places that you're doing all the things.
- 4:14But, you know, tell us about yourself.
- 4:17Feel free to mention your book so people know about them as well.
- 4:19There you go.
- 4:21Yeah, there's an old joke.
- 4:22How do you know somebody runs a marathon?
- 4:24Well, they'll effing tell you.
- 4:25Well, authors are more or less in the same package.
- 4:28It's a contractual obligation.
- 4:30It is.
- 4:31I wrote it about in my book, Chris.
- 4:32There you go.
- 4:33Exactly.
- 4:34That's right.
- 4:34And I guess while I've got you on air, you know, thank you for writing the foreword.
- 4:39So yeah, I guess now I should sell myself as an author.
- 4:46It's nowhere near as impressive as people think it's sound to be.
- 4:50I guess I spend my spare time doing sort of Python education-y stuff with you and other
- 4:56organizations, and writing is kind of part of that.
- 4:59My day job is still helping organizations do technical stuff.
- 5:04Sometimes that's architectural advice, and sometimes that's things like Agile and Lean
- 5:09process driven things. My marketing people tell me to sell myself as a fractional CTO.
- 5:15I have a hard time saying that with a straight face, but you know,
- 5:18got to put it on the brochure. So there you go.
- 5:20Well, if you don't want to take it so seriously, you could be an improper fraction.
- 5:24There you go.
- 5:25You could be like a two ace CTO.
- 5:28I'm an irrational CTO. That actually makes sense.
- 5:34I'm radical two over seven. Let's go. That's how much a CTO I am.
- 5:38More seriously, though, I'm sure there are people listening who find that idea pretty interesting, fractional CTO or sort of fractional something beyond just like low level coder.
- 5:48Tell people what that what is that like?
- 5:50How do you get into that?
- 5:51I got into it because I kind of fell into software management early on in my career.
- 5:57There was a company I was at where I was it was a Windows based shop and I was hired as the Unix guy.
- 6:05We all had this naive idea that we could port the product to Unix easily.
- 6:10So I was their sole Unix architect person.
- 6:14After about six months worth of work, we figured out this just wasn't going to happen.
- 6:18And my VP got pregnant and went, want to be a manager?
- 6:21And basically handed me the keys.
- 6:24So I kind of learned by being thrown in the deep end as to how to do the technical management type stuff.
- 6:30So my career has always kind of been a bit of a balance between that.
- 6:33I like smaller organizations and I like helping them grow. So what that tends to mean is when it's really early on, when you're number two or three, you're coding like a line developer. And as the organization grows, you spend less time doing that. And because I've spent a lot of time in startups, I've done that journey many times.
- 6:50So the fractional CTO idea essentially is to try to help smaller organizations that wouldn't necessarily be able to afford a full-time CTO with that kind of thing and sort of provide the kind of advice from things I've learned throughout that process over the years and then sort of help out as you go along.
- 7:09And sometimes that's come in with a very specific target of help us architect something.
- 7:16And sometimes that's more of a, we need some, it's sometimes referred to as adult supervision,
- 7:23where somebody comes in and just sort of offers sort of the process advice, right?
- 7:30Right.
- 7:31Who's going to be this adult in the room that says, no, we're not rewriting that in a new JavaScript framework.
- 7:36It was a third time this year.
- 7:37Yeah. And often it's very hard in a startup to concentrate on things like CI/CD and testing and all the rest of it because you're trying to get to market. So it's all about those features. Well, if you don't balance that, it's going to bite you later on. Right. So I try to help with sort of that overall bigger picture as part of the organizations and get them going.
- 8:00Cool. Yeah, I find even outside of that kind of stuff, sometimes it's hard to focus on the boring but important features that matter rather than like, hey, wouldn't it be cool if we refactored this or made that faster? Like, it doesn't need to be faster yet until you have more customers. What you need is this other thing, this PDF export that nobody wants to write.
- 8:18Yeah. And as developers, you know, like the entertaining part of our job is learning the new thing. And oftentimes that's learning the new library. And sometimes that's great. And sometimes that's wasting a whole bunch of time because the old library would have been just as good and keep sort of plowing along. Right.
- 8:35There's a phrase that I stole in my book.
- 8:38See, I keep bringing it back to the book.
- 8:41People talk about Django being old and it's like the difference between, you know, it's a dinosaur.
- 8:45No, no, it's a shark.
- 8:47They're still around.
- 8:48They're still valuable.
- 8:49They're just as old as the dinosaurs, but they're still part of the ecosystem.
- 8:56Interesting.
- 8:57Hopefully, you know, the gray hair at the temples here helps me provide a little more insight into that than, say, somebody fresh out of school.
- 9:07Mm-hmm.
- 9:07Yeah, you see those gray hairs?
- 9:09That's C++.
- 9:10Exactly.
- 9:14Yeah, indeed.
- 9:14Okay.
- 9:15Well, let's talk data science and let's talk Polars.
- 9:18So I guess there's a lot of people who have heard of Polars and are experts.
- 9:23I see out in the live audience already.
- 9:25There are some folks whose things they've created for Polars we're going to talk about in this show, actually.
- 9:31So some people need no introduction to Polars.
- 9:34But there are many people listening to the show who are just getting into Python, using this as one of the footholds to kind of get a feel for the space.
- 9:42And they might go like, what is Polars?
- 9:44Sure.
- 9:45You know?
- 9:45And what's a data frame?
- 9:47Like, tell us about this.
- 9:48Yeah, I was about to say, before we get to the Polars, maybe we should just start with a data frame.
- 9:52So a data frame is kind of an in-memory spreadsheet sort of thing.
- 9:55It's a data structure that consists of rows and columns.
- 9:58And you do the same kind of things that you might do in Excel, but in your code.
- 10:04There are a whole bunch of advantages to doing it in your code over doing it in Excel.
- 10:07And in fact, the course that we're going to talk about gets into some of that.
- 10:11But there are a bunch of different libraries out there that help you do this.
- 10:14The big one that has been around for probably the longest and is the most popular is Pandas.
- 10:21Pandas has made some interesting design decisions.
- 10:25And as a result, there are some other more recent additions to the space that are trying to address some of those things that Pandas has done.
- 10:34And Polars has become one of the more popular ones.
- 10:37I saw just as we kicked in here, there was a comment already about, I like the coding style of Polars better than Pandas.
- 10:43And this to me, this is also why I actually prefer Polars.
- 10:47The interface to it is a lot more like object-oriented code, and it feels more Pythonic to me.
- 10:56And as a result, it tends to be one of the libraries that I now head towards if I need to muck with a bunch of data and do those kind of data framey things.
- 11:05It is almost every benchmark out there has marked it as being significantly faster than Pandas.
- 11:12So that's a nice little advantage.
- 11:14And it also includes methods to convert your data into pandas and other data frames as well.
- 11:22So if you do get stuck and there's something that isn't there and because it's a newer library,
- 11:27you can always just go, oh, give me the pandas version of this, run my pandas commands,
- 11:31and then bring it back into the polar space where you make it a little more comfortable.
- 11:35So it's a nice...
- 11:35Right, it's two pandas and a from pandas sort of thing, yeah.
- 11:38Yeah, and so it's a nice flexible sort of approach and it's a quick bear.
- 11:43And one of the things I find, I'm not trying to dump on Pandas.
- 11:48It's going to sound like that occasionally, but I really do.
- 11:51I like the library.
- 11:52It's very, very powerful, very, very useful.
- 11:54It uses a lot of black magic.
- 11:56And I find as a developer, as somebody with a software engineering background, sometimes
- 12:02code can be distracting where you look at it and go, how does that even compile?
- 12:05What is that?
- 12:06What are they doing?
- 12:07And that pulls me away from the, I'm trying to do something.
- 12:11And I find some of the stuff in Pandas kind of falls into that bucket.
- 12:14Whereas you just do nice little chained function calls in Polars.
- 12:18And so you're reading, you know, filter, select kinds of things.
- 12:22And it feels a lot cleaner to me.
- 12:25So it tends to be where I want to go.
- 12:28And then the other big advantage of it, which some of the other data frame libraries have,
- 12:33but Pandas, as far as I know, doesn't, is lazy evaluation.
- 12:37And this is the idea of rather than executing each operation as you call it, you can chain a bunch of things together and then call them all at the same time.
- 12:46This tends to give you large amounts of speed up.
- 12:49So, for example, if you're reading in a CSV file and you want to do something only on certain rows in that CSV file, the filter can throw those rows that aren't important.
- 12:58And then you would only run the operation on those rows that you were interested in.
- 13:03Of course, that tends to be an awful lot faster than going through every single row at a time.
- 13:08It also tends to use less memory for the same reason, because you don't have to keep as much in memory at any given time.
- 13:15So lazy evaluation can be a really, really powerful tool, particularly if you're using with really, really large data frames.
- 13:22So that to me is, you know, the coding style was a nice selling point.
- 13:27But being able to see that performance difference with the lazy evaluation was really what hit it home for me.
- 13:33Yeah, they're both super cool. You know, I talked to Richie Bink, who is a creator of Polars, about three years ago or so on the podcast. And it was very interesting to talk about some of the philosophies. So, for example, the lazy evaluation is great because you could theoretically change the order. Like if I'm going to do some complicated math computation on a thing, we'll talk about some of those.
- 13:56And then I'm going to filter it down based on some unrelated criteria like state or whatever, gender, who knows, and then get the answer.
- 14:05If I did that in pandas, it would do the math on every single thing and then filter it down and give you the answer.
- 14:10And maybe take the US as an example, that might be one fiftieth of the data that you wouldn't have to multiply.
- 14:16So with Polars, it can even optimize.
- 14:19Well, if there's a filter, do the filter first and then the calculation.
- 14:22Yeah.
- 14:23So it's got kind of an optimizer built in there as well.
- 14:26This technology is very similar to what happens in databases.
- 14:30So if you're doing more complex SQL, behind the scenes, the database for you goes and tries to optimize that to try to figure out what the best way of doing this for you.
- 14:39And Polars has that built in as well.
- 14:42It's got it built in on your calls specifically.
- 14:46So you can take advantage of that even if it isn't the lazy evaluation.
- 14:49But it can really, really shine in the case of lazy evaluation.
- 14:52So if you're doing a complex query all in one chunk,
- 14:55like a group by with a bunch of different aggregates,
- 14:57it does still do the same thing,
- 14:59which again gives you one of the ways that it'll speed up.
- 15:02But particularly with lazy evaluation
- 15:05where you might chain together 15 or 20 calls,
- 15:08it'll figure out for you what the best order on that is
- 15:11and that can make a big difference.
- 15:12You can also see what it wants to do
- 15:16and what its execution plan is.
- 15:19I don't understand the output.
- 15:24it's deep relational algebra
- 15:27but for those who are into this stuff
- 15:29you can actually see exactly what it's doing
- 15:31and it gives you a little peek behind the curtain
- 15:36this portion of Talk Python I Me is brought to you by Sentry
- 15:40over at Talk Python
- 15:41Sentry has been incredibly valuable
- 15:43for tracking down errors in our web apps
- 15:46our mobile apps and other code that we run
- 15:48I've told you the story how more than once
- 15:50I've learned that a user was encountering a bug through Sentry and then fixed the bug and let them
- 15:55know it was fixed before they contacted me. That's pretty incredible. Let me walk you through the few
- 16:00simple steps that you need to add error monitoring and distributed tracing to your Python web app.
- 16:06Let's imagine we have a Flask app with a React front end and we want to make sure there are no
- 16:10errors during the checkout process for some e-commerce page. I don't know about you, but anytime
- 16:16money and payments are involved, I always get a little nervous writing code. We start by simply
- 16:21instrumenting the checkout flow. To do that, you enable distributed tracing and error modern train
- 16:26in both your Flask backend and your React frontend. Next, we want to make sure that you have enough
- 16:32context that the frontend and backend actions can be correlated into a single request. So we enrich
- 16:38a sentry span with data context. In your React checkout.jsx, you'd wrap the submit handler in a
- 16:46ban call. Then it's time to see the request live in a dashboard. We build a real-time Sentry dashboard.
- 16:51You spin up one using span metrics to track key attributes like cart size, checkout duration,
- 16:56and so on, giving you one pane for both performance and error data. That's it. When an error happens,
- 17:03you open the error on Sentry, and you get end-to-end request data and error tracebacks to easily spot
- 17:09what's going on. If your app and customers matter to you, you definitely want to set up Sentry like
- 17:14we have here at Talk Python.
- 17:16Visit talkpython.fm/sentry
- 17:18and use the code talkpython,
- 17:20all caps, just one word.
- 17:22That's talkpython.fm/sentry,
- 17:25code talkpython.
- 17:26Thank you to Sentry for supporting the show.
- 17:30With databases,
- 17:31there's usually some way to ask for
- 17:33what's called an explain.
- 17:35Like you got some query,
- 17:36instead of saying run the query,
- 17:37parse it and do the optimizer
- 17:39and then tell me what you would do.
- 17:40And that can be really helpful
- 17:41for saying like finding an index or something.
- 17:43Yeah.
- 17:44Is it actually using this index or is it not?
- 17:46But then there's something like that in polars as well, yeah?
- 17:48Yeah, and it calls it explain as well.
- 17:50So when you've got the lazy object, you can call explain on it and it'll show it.
- 17:55It also has a graphical equivalent, so it'll spit out a little image,
- 18:00that sort of flow chart-y kind of thing that shows you what it is as well.
- 18:03So if you prefer the pictures over the obscure Greek letter references,
- 18:08then that can show you what's going on.
- 18:11Yeah, very nice, very nice.
- 18:12All right.
- 18:13Well, we are going to not talk about the exact subject of your course that you wrote, but I think we should maybe give it a quick shout out.
- 18:23So your course that you wrote at Talk Python is Polars for the Power Users, Transform Your Data Analysis Game.
- 18:29What we're going to talk about is a bunch of different cool tools that extend and power up polars.
- 18:35But if people are interested in learning polars more from scratch, they could take your course.
- 18:39So I'll be sure to link to that in the show notes.
- 18:40Maybe give people a super quick rundown on what's on the course, and then we'll talk about all the items.
- 18:47Sure.
- 18:48As you might guess from the title, it kind of teaches your Polars.
- 18:51And you can see on the screen there, there's a bunch of different icons as well.
- 18:56What we're trying to do is essentially take you to that next step.
- 19:01So if you're doing data science-y stuff in Excel and you're trying to figure out how to move to the Python world with this,
- 19:09then this is kind of a course to try and help you do that.
- 19:13And it does it through the Polars library.
- 19:15So there's a whole bunch of different examples as you go along.
- 19:18A lot of the examples will start with, hey, here's a spreadsheet.
- 19:21Here's a thing that you're doing in the spreadsheet.
- 19:23Here is how you would do that with the Polars library and Python code instead.
- 19:29And why you might do that instead of doing a spreadsheet.
- 19:33Polars has this concept of expressions and filters.
- 19:38And they're the ways of performing operations on your data and then looking at different columns or different rows.
- 19:44So the course starts out there and then starts getting even deeper into real world examples.
- 19:51The final lesson is actually a case study.
- 19:54We took a bunch of the GDP data from the United Nations and showed you how to clean some of it, how to combine it.
- 20:05whenever you've got data in the real world,
- 20:07even if it's from the same source,
- 20:09you're going to go,
- 20:09oh, I want this information from this column
- 20:11and that information from that column,
- 20:13but they aren't related.
- 20:14So now we have to cross-reference.
- 20:16So it covers how to sort of do all of that kind of stuff.
- 20:18So it's sort of this journey of
- 20:22how do you take some of the more basic CSV
- 20:24and Excel type stuff and change it into Python
- 20:27all the way along the line
- 20:28to how do we actually create a report
- 20:31on data from the real world
- 20:33using Polars as your library for doing it.
- 20:36Excellent.
- 20:37Yeah, it's also very funny.
- 20:38A very funny course.
- 20:39I enjoyed it.
- 20:40Okay.
- 20:41Polars is pretty awesome, wouldn't you say?
- 20:42Yes, that's a nice, you know, there you go.
- 20:46How's that for a segue?
- 20:48Pretty awesome.
- 20:49In fact, let's reverse that.
- 20:51We're going to talk about awesome Polars.
- 20:52So there are so many of these awesome lists out there.
- 20:56And, you know, if you are interested in something
- 20:58and you haven't looked yet for an awesome list for it,
- 21:01go do that.
- 21:02There's got to be an awesome Django.
- 21:04I know there's got to be an awesome Flask.
- 21:06There's an awesome Async.
- 21:07There's an awesome Python.
- 21:09And Ddata has gone and created awesome Polar.
- 21:12So what we're going to do is we're going to go to this curated list
- 21:16and pull out 10-ish number of things that we think would be really interesting.
- 21:22On the order of 10, yes.
- 21:22We didn't count.
- 21:24One of these days we'll start prepared.
- 21:26Well, my tabs scroll off the screen, so I can't even count them.
- 21:30So there we go.
- 21:31But no, something on that scale.
- 21:32We're going to go through a bunch of different little things that are great.
- 21:34And for me, I just, like I said at the opening of the show, I really like these because it's
- 21:37like, oh, it's not a big deal, but it just made all of my work way easier and is really
- 21:42easy to adopt.
- 21:42A lot of these things are actually not huge commitments.
- 21:46It's not like redo your data stack in some other data frame library because you'll get
- 21:50some benefit.
- 21:51Like, no, you just plug this thing into your puller's work you're already doing and it's
- 21:54really good.
- 21:55So with that, let's start out on visualization.
- 22:00What do you think?
- 22:01Sure.
- 22:02Yeah, and this one's almost independent of Polars.
- 22:05I guess there's sort of this whole category of things that are already in the data science space.
- 22:12And what is specific to Polars is the good news, which is Polars works with all those tools that you're used to working with.
- 22:19So if you're already playing with Matplotlib or Seaborn or any of those, Polars will integrate with it just like Pandas does and most of the others.
- 22:27Polars does have sort of specific integrations with Altair and HVplot.
- 22:33So there's built-in calls on the data frame that essentially say,
- 22:36go plot this and they will use those tools.
- 22:38But even if you don't want to use those,
- 22:41getting at a column, slicing a column out,
- 22:44and then handing that column down to Matplotlib works more or less just like Bandus does.
- 22:49So this integration works quite nicely.
- 22:52Likewise, with tools like Jupyter or Marimo,
- 22:55again, this isn't really Polar specific,
- 22:57but there's nothing in Polars that stops you from using these tools.
- 23:00And so, you know, you're not, you know, if you're moving from pandas,
- 23:07this isn't a stopper.
- 23:09All the other things, you don't have to learn everything else from scratch.
- 23:12Everything you bring with you will continue to work without any problem.
- 23:16Yeah, so on the Polars docs, we've got examples for HVPlot, MapPlotlib,
- 23:21Plot9, Seaborn, Plotly, all these things.
- 23:23And yeah, they look great.
- 23:24Have you played with Altair much?
- 23:25You know, I've not played with Altair much. I think I've done mostly Plotly and Seaborn lately.
- 23:31Okay. Yeah. I still, I have a love-hate relationship with Matt Plotlib. I'm usually too lazy to learn anything else and I grumble every time I use it because I can't remember how to set X underscore or set underscore X and it drives me crazy. But it gives you results and that's what's important.
- 23:50For sure. So you mentioned it, Marimo. I recently covered, talked with Akshay from the Marimo team. I'm impressed. I am super impressed with the way that Marimo came out, the way it looks. It seems like a really nice, just a very nice modern Jupyter style. What do you think of it?
- 24:09So I've yet to play with it. I've done a bunch of reading on it. The theory behind it is something
- 24:15that is beautiful to me. I actually don't use Jupyter. And the big thing, this big stopping
- 24:20thing for Jupyter for me is it doesn't feel like it scales. And that's because the cells are stored
- 24:26as JSON. So it's a great tool like a REPL where if you're experimenting with something, it's
- 24:31fantastic. But as soon as you're trying to start doing that on a team, it can be problematic because
- 24:36now you're trying to merge those JSON pieces. It doesn't work very well. And Marimo has essentially
- 24:42tried to solve this from the ground up. Instead of creating JSON to store everything, it's actually
- 24:48creating Python output, which means the documents that you're creating in Marimo actually can be
- 24:54managed just like any other code. And although I haven't played with it myself, the theory behind
- 25:02that to me is very, very beautiful. And the next time I dive into this space, this is definitely
- 25:06going to be the one I'm going to be playing with. Yeah, it looks great to me too. I think even just
- 25:11from a visual, like the way the UI works, I really, really like it. The other thing in addition to the
- 25:17Python backing instead of the JSON backing in, as in file storage, is the reactive bits. Like one
- 25:25of the things that always scares me about notebooks is like it can go one, five, two, seven. Yeah.
- 25:31And you don't even know what happened
- 25:33into the like number six one, right?
- 25:35But it affects the way the notebook runs.
- 25:38Like it's kind of like go-to statements in it,
- 25:40but you get to pick randomly.
- 25:42And this has a way to say these cells depend in this order.
- 25:44And so there's a automatic refresh and stuff.
- 25:47Anyway, bit of a diversion,
- 25:49but I think it's worth paying attention to.
- 25:51Okay, where were we again?
- 25:52Well, next thing we want to talk about is DataFramely.
- 25:58Claritive, Polars native data frame validation library.
- 26:02So the data science, the first 80% of your work is cleaning the data and getting it into the right shape.
- 26:09And then the second 80% of your work is coding.
- 26:12And then there's that last 80% of the work, which is output.
- 26:16I think my math might be off there.
- 26:19It feels like it's right, though.
- 26:20It feels that the reality is true, though.
- 26:24And what DataFramely helps you do is that first 80%.
- 26:29So this is from a group of folks at a company called Quantco,
- 26:33and they're a data science consulting firm.
- 26:36And it works a bit like Pedantic or DjangoRM.
- 26:40See there, I brought it back.
- 26:42There's a book on that.
- 26:44So you can create a data class-like object here,
- 26:47which inherits from DataFramely's schema class.
- 26:50And inside of that, you declare what your expected columns
- 26:53in a data framework.
- 26:55So for example, you could specify a weight,
- 26:58something that is a weight,
- 27:01like heavier light is a float value
- 27:04and an American zip code is a string.
- 27:06And in addition to that declaration syntax,
- 27:09you can also write rule methods on the class
- 27:12to give you more complex validation code,
- 27:14like this plus this have to be,
- 27:18these two values have to work together
- 27:19or these two values have to be unique
- 27:21or those kinds of validation pieces.
- 27:23Now, once you've got one of these schema objects, you then call its validation method, passing in your data frame.
- 27:29And if the results are clean, you get back your data frame.
- 27:31And if they aren't, you get an exception that describes each validation, telling you what rule failed, what rows it failed upon.
- 27:39And there's also a soft validation mechanism called filter, which returns a data frame with the rows that passed and a failure info object with the rows that went wrong.
- 27:50So if you don't want to just sort of stop because it's too bad, you want to react to the parts that aren't, you can dig in and sort of go from there.
- 27:59So this is a nice little piece and it can definitely help you.
- 28:04It doesn't fix the dirty data, but helps you detect it, which of course is your sort of first step as you go along.
- 28:10Yeah.
- 28:11One of the real challenges you can run into is the data, it parses or something, right?
- 28:18Yes.
- 28:18And so then math works.
- 28:20But at the same time, that's not necessarily giving the right answers.
- 28:24And you've got to figure out, well, why is this not working?
- 28:27Yeah.
- 28:28In fact, in the course, one of the examples we use, which is what you've got up on the
- 28:31screen here as well, is oftentimes people will put an American zip code, which for our
- 28:36non-American friends is a five-digit number or could be a nine one, which that just makes
- 28:41it messy.
- 28:42But let's stick with the five digits for now.
- 28:44Well, it could have a leading zero.
- 28:46Well, if you've stored that in, say, Excel as a number, then that leading zero gets chopped.
- 28:52And now all of a sudden you've got this four digit thing, which isn't a valid zip code.
- 28:56And so as part of your data cleaning, you have to detect that or as part of loading the file.
- 29:01And you might have to say, hey, wait a second.
- 29:03This shouldn't be an integer.
- 29:05Please load this as a string.
- 29:07And so what these validation mechanisms allow for you is to help you sort of catch this.
- 29:12And so, for example, if you were processing that failed info object and the only things that failed were zip codes, well, then you could have some code in there that went, okay, I'll just make sure that I fix the padding on this and maybe deal with it automatically as part of your data pipeline.
- 29:28Yeah, you can do something like this to catch it potentially.
- 29:32This portion of Talk Python To Me is brought to you by Agency.
- 29:35The Agency, spelled A-G-N-T-C-Y, is an open source collective building the Internet of Agents.
- 29:42We're all very familiar with AI and LLMs these days, but if you have not yet experienced the
- 29:48massive leap that agentic AI brings, you're in for a treat. Agentic AI takes LLMs from the world's
- 29:54smartest search engines to truly collaborative software. That's where agency comes in. Agency is
- 30:00a collaboration layer where AI agents can discover, connect, and work across frameworks.
- 30:06For developers, this means standardized agent discovery tools, seamless protocols for interagent communication, and modular components to compose and scale multi-agent workflows.
- 30:18Agency allows AI agents to discover each other and work together regardless of how they're built, who built them, or where they run.
- 30:25And they just announced several key updates, including interoperability with Anthropics Model Context Protocol, MCP,
- 30:33a new observability data schema enriched with concepts specific to multi-agent systems,
- 30:38as well as new extensions to the OpenAgentic Schema Framework, OASF.
- 30:44So are you ready to build the Internet of agents?
- 30:47Get started with agency and join Crew AI, LangChain, Llama Index, BrowserBase, Cisco, and dozens more.
- 30:54Visit talkpython.fm/agency to get started today.
- 30:57That's talkpython.fm/agency.
- 31:00The link is in your podcast player's show notes and on the episode page.
- 31:04Thank you to the agency for supporting Talk Python.me.
- 31:08It's weird the shadow that Excel casts, especially onto the data science world.
- 31:14I'm thinking of how, or even science in general, there was some gene as in...
- 31:18I was just about to mention the same example.
- 31:21I think it was like MAR223 or something like that.
- 31:25But if you put that to Excel, it's like, oh, March the 2nd?
- 31:28You're like, no, no, no, no, no.
- 31:30Yeah, yeah, yeah.
- 31:32And in fact, even Polars to a certain extent has some of those kinds of problems.
- 31:40The routine that you use to read in a CSV,
- 31:42because CSV files don't actually have data type information inside of them,
- 31:46it essentially makes a guess.
- 31:48And it uses, I think by default, it's like the first thousand rows of the file to make an educated guess as to what the data types are.
- 31:56And what can happen is, let's say your data is consistent in the first thousand rows and then row 1001 has something else.
- 32:04Well, then it's going to die because it'll say, well, this is an integer and now you're giving me a float.
- 32:10And because it didn't detect that this was supposed to be a float in the first place.
- 32:14Now, the difference between Polars and Excel is Excel will just keep chugging along and
- 32:20you may not discover that you've caused yourself this problem, whereas Polars actually expects
- 32:25those columns to be a data type.
- 32:26So it'll scream.
- 32:28And part of your data cleaning process would be to catch those kinds of things and fix
- 32:32that.
- 32:32Yeah.
- 32:33I mean, spreadsheets are awesome.
- 32:35They're really cool, but they get overused.
- 32:38Like I was doing something where I had a bunch of dates in a column and I'd selected all
- 32:41of them just to copy them.
- 32:43But, you know, at the bottom it says, here's the sum of this or whatever.
- 32:46It told me like the sum of these 20 dates was like March 12th of some.
- 32:52I mean, nope, don't think it is.
- 32:53But, you know, okay.
- 32:54If you.
- 32:55Well, the beauty of the spreadsheets is they're very, very flexible.
- 32:59And the pain of a spreadsheet is they're very, very flexible.
- 33:03And so, you know, it's, yeah, you could end up missing some toes depending on where you're pointing the gun.
- 33:09So one of the challenges, for example, with your zip code example is maybe the first thousand rows are West Coast people.
- 33:15So every number looks like a five digit proper number.
- 33:19But after that, you get to East Coast people, which start with zeros because it goes old to new, I guess, is the way they came up with it.
- 33:27And that could be a challenge, right?
- 33:28Even just simple stuff like parsing that.
- 33:30OK, what is up next?
- 33:32So we go on the patio and have this conversation.
- 33:34Yeah, so essentially, just as a quick aside, there are a couple different libraries that essentially do the same thing as what we just talked about.
- 33:41Patio, Petito, I'm not sure how you're supposed to say this, which wasn't actually on the awesome pullers list, but it's one I've come across before.
- 33:50This is by Jacob Gerard Martinson, and it's built on top of Pydantic.
- 33:55Like with DataFramely, you declare a class inheriting from a model, then validate against it.
- 34:00The twist on this one that I kind of liked is it also has a mechanism for generating data.
- 34:06So if you're writing unit tests, once you've got this validator, you can go through and say, oh, give me some data to unit test with.
- 34:12And it will give you data that complies with your model.
- 34:16So I thought that was kind of an interesting little twist for helping you build your pipelines.
- 34:21Yeah, it's super neat.
- 34:22If you're a fan of Pydantic, this looks really, really cool.
- 34:25You get basically the type of errors you would get parsing JSON with a Pydantic model.
- 34:31It's the exact same error messages you get, but for data frame validation.
- 34:34So I think that that's pretty cool.
- 34:36Yeah, so it's nice and familiar.
- 34:37Yeah, definitely.
- 34:38If you're a FastAPI person or Pydantic for whatever reason.
- 34:42Also, Patito, not patio, I suspect it probably is.
- 34:46I want to start a trend.
- 34:47I want to try to put something out there in the world for people.
- 34:50If you've got something that could have multiple pronunciations, put just a little MP3 or something in your repo.
- 34:58It sounds like this.
- 35:00You know what?
- 35:01I'm not sure that that's helpful because you remember the whole how do you pronounce Linux debate back in the 90s?
- 35:08One of the, I think it was Slackware distribution I had, came with an MP3 of Torvalds pronouncing it.
- 35:15And of course, with his accent, it didn't solve the problem.
- 35:17So I'm not sure that MP3 is actually the answer.
- 35:22Maybe just a little hyphenated where you want to put the stress of it.
- 35:26So yeah, I think I saw a GIF or was it a GIF about it?
- 35:30I don't know.
- 35:30Yeah, exactly.
- 35:31That's right.
- 35:32Just something to help us figure it out.
- 35:35That gives us a fight about on the internet.
- 35:37Yeah, that's right.
- 35:38Let's make memes about it.
- 35:39Okay.
- 35:40So the next one is Polars IP tools.
- 35:42Pretty sure that's how we'd say this for you.
- 35:44Yeah, this one's less debating.
- 35:46Yeah.
- 35:46So this is a library by Eric Hutchins.
- 35:49And if you're playing with IP addresses, this could be helpful to you.
- 35:53It essentially provides a bunch of data frame methods for mucking around with IP addresses in a column.
- 35:59One of the functions it provides is private.
- 36:03This returns true if the address is a private network.
- 36:06So if, you know, 192, 168 or one of those groupings that's private, then you get true or false based back from it.
- 36:12Another method is in.
- 36:15So you give it a list of addresses with masks, and then it'll give you true or false for every IP address as to whether or not it's in that specific group of ranges.
- 36:25The library also integrates MaxMind's GeoIP address database, if you've got access to that, as well as the Spur VPN database, which contains information about what's a proxy, what's a VPN, that kind of stuff.
- 36:37So if you're doing a whole bunch of log management and you're dealing with all those IP addresses
- 36:44that are coming in your logs and you want to use pullers to muck around with that and create
- 36:48reports, then IP tools might be helpful for some of that kind of stuff.
- 36:51Yeah.
- 36:51If you're doing online sales, you want to know where the sale is coming from or where's
- 36:55the interest coming from, say for marketing, this would be super valuable.
- 36:59Once a sale happens, often you can say, well, they put their address or zip code or credit
- 37:04card information we can see that but there's way more traffic upstream from that that doesn't
- 37:08necessarily buy things right you can see where the interest is and and so on i actually don't use this
- 37:13library but i use the max mine stuff over at talk python because when you come to visit a course i
- 37:19want to show it in your currency well what is your currency well it belongs to your country
- 37:23what is your country you probably haven't told me but i know your ip address so let me see if i can
- 37:27figure that out and at least get it right most of the time so yeah this is cool right fuzzy match
- 37:33So this is great.
- 37:34Who's geese?
- 37:35Which is it?
- 37:35Which am I finding?
- 37:37Exactly.
- 37:37So from moving from IP addresses, which are very specific strings to some more generic ones,
- 37:44the Polars Fuzzy Match Library does kind of what you would expect from its name.
- 37:48The GitHub user's name was bnmock3.
- 37:52He didn't write it as his full name.
- 37:53So that's what we're going to go with.
- 37:56His avatar or their avatar is delightful.
- 37:59iSquare symbol non-ASCII characters, which is funny. So if you're not familiar with fuzzy matching,
- 38:06it's about approximate matches on a string. So this library comes with a function that returns
- 38:12a score based on the thing that you're matching. So let's say I had a series of strings that I
- 38:16wanted to match against the word car, and that list had care and card in it. Those are close,
- 38:23so they might have a score of like 80 something. Well, frog, which has nothing in no relationship
- 38:29shift a car that scores a null. So this allows you to sort of see how close you are. And the fuzzy
- 38:37matching routine that they're using is based on an underlying Rust library called Nucleo,
- 38:43which comes out of the Helix editor. So it's supported in a whole bunch of different ways.
- 38:48This isn't just some random guy on the internet. And it also has a bunch of strength, a bunch of
- 38:54ways of specifying patterns like submatches starts with suffix matches, inverted matches,
- 39:01and more. I kind of like the suffix match feature. So it allows you to do fuzzy matching just on,
- 39:06say, the suffix part of the extension part of a file name and ignore the rest. So you can,
- 39:11you know, whether they mucked up MP3 or MP4 or whatever. So that's kind of a neat little piece.
- 39:17And then along the same lines, the next one's Polars STR-SIM.
- 39:23It is, so fuzzy matching is based on something called a distance calculation.
- 39:28And this essentially takes two strings and then gives you a value for,
- 39:33typically it's between one and zero as to how close they match.
- 39:37So if they're identical, it gives you a one.
- 39:39And if they have nothing in column, it gives you a zero.
- 39:43The most popular of these is Levenstein distances, but there are other ways out there.
- 39:49The Jacquard, for example, or the Sorensen dice or whatever.
- 39:54Yeah, I wasn't even going to attempt that.
- 39:56So thank you.
- 39:59These kinds of calculations get used in things like, say, spell checking software.
- 40:02So if you've got something that isn't in the spelling dictionary and it wants to make a
- 40:07suggestion, then what it does is it runs one of these kinds of distances on a bunch of
- 40:11words in the dictionary and those values that are close are the suggestions that it'll give you.
- 40:16So the Polars STR sim library, which is from a fellow Canadian, Jeremy Foxcroft,
- 40:21allows you to calculate these string distances on your data frames. As you mentioned, it supports a
- 40:28whole bunch of mechanisms, which I'm not even going to pretend to pronounce. And each of the
- 40:34calculators basically takes two column names and then calculates the distances between them,
- 40:39which makes it really easy to include in something like a with_columns call
- 40:43so that you can just sort of add this to your data frame
- 40:45and go along with your distance information.
- 40:47So if you're trying to do partial matching,
- 40:50either of these two libraries could be helpful for you.
- 40:52Yeah, they seem really great.
- 40:54Just a little bit of a side note here.
- 40:55I think this is an interesting pattern with this Nucleo Rust library
- 40:59that is actually kind of the foundation of the Polars Fuzzy Match, right?
- 41:04I think this is something people should kind of keep their eye on out there.
- 41:08like okay so this is not super popular but you might think well maybe it's not going to work that
- 41:14well but if it's just kind of a wrapper around something that is super popular right an example
- 41:19would be um granian i had giovanni on the show to talk about the granian python production worker
- 41:27process thing and it's getting pretty popular 3.5k but you know you look at say uveacorn or something
- 41:33like that but then really what it is kind of a wrapper around hypercrate well hypercrate is way
- 41:38popular written in rust right used by 360 000 projects and 400 contributors like oh okay so
- 41:45brand is kind of like an interface to hyper in a sense right and that's also a little bit what's
- 41:50going on here and so when you're evaluating these libraries i think that's worth keeping in
- 41:55mind what do you think yeah no for sure like it's um you know not that you can't not that you can't
- 42:00create bugs in in a wrapper because you can create bugs in anything but uh you know the less code the
- 42:06less likely there's a problem. And, you know, if it's built, if it, like you say,
- 42:10if it's just a thin veneer on something that is big and popular and well-maintained, then,
- 42:15you know, the fact that it's a thin veneer becomes less, less important.
- 42:18Right. Right. Like, well, that veneer is not that popular. Like that veneer doesn't matter
- 42:21that much. Okay. Maybe you should encrypt it though. If it had some sort of data,
- 42:25you should encrypt it.
- 42:26There you go. And this, this is another one actually that, you know, we'll pretend we
- 42:30planned that. so this is another one, which is a thin veneer. so if you want to be sneaky
- 42:35with your text here.
- 42:38Niazi Garagashli,
- 42:39which I'm sure I'm butchering,
- 42:41has written Polars Encryption.
- 42:43This library incorporates AES ciphers
- 42:45into Polars,
- 42:46providing encrypt and decrypt functions
- 42:48as expressions.
- 42:50So if you're mucking around
- 42:51with, say, personally identifiable information,
- 42:55or you're just like sending
- 42:56other data scientists secret messages
- 42:57about your pointy-haired boss,
- 42:59then this little veneer
- 43:00could actually be fairly useful to you.
- 43:02Well, I'm thinking of maybe there's
- 43:05encryption at rest type of stuff you want to be having going on.
- 43:09You know, there's a lot of different file formats that you can save in, right?
- 43:13I mean, there's CSV or Excel, but you don't really want them necessarily.
- 43:16You're doing like native folders, right?
- 43:18We have Parquet, PyArrow, other better types, right?
- 43:23So maybe you import some data and you then encrypt it.
- 43:26So that file sitting on disk, somebody grabs it like, so what?
- 43:30I mean, your app still has to know the encryption key
- 43:32and maybe decrypts it in a data frame and memory.
- 43:34But it's a really nice sort of back and forth library for that, right?
- 43:37Yeah, and data frames are a perfect place to do that kind of thing.
- 43:40Prescription tends to be expensive,
- 43:43but data frames tend to be fairly fast about that kind of stuff.
- 43:48So you're going to outperform using a mechanism like this
- 43:51that's built underneath as a Polaris plug-in
- 43:53than, say, building a loop yourself and doing it.
- 43:57So it tends to, like you said, it gives you that safety,
- 44:00but it gives you it in a way that's a little speedier than doing it yourself.
- 44:04Yeah, I don't remember if I spoke to Richie about this.
- 44:06Maybe I did, but I don't remember what he said if I did.
- 44:09That's for sure.
- 44:11Does Polars do parallelism automatically,
- 44:15or is there a way to ask it to do it easily, right?
- 44:17Because this encryption per entry in a column
- 44:20is one of the embarrassingly parallelizable algorithms, right?
- 44:24I know there are async pieces underneath.
- 44:26I haven't played with them myself,
- 44:28so I'm not sure exactly how you trigger that.
- 44:32I know, so Polars itself, although it's very much a Python library, the part of it underneath is still Rust.
- 44:40And I believe that has been built so that you can get some of that concurrency.
- 44:44I don't know how easy it is to just turn that on.
- 44:47It hasn't been something, you know, one of the things with concurrency, it's actually what I did my master's degree in.
- 44:55And the only thing I really remember is really, really don't do it unless you absolutely have to.
- 45:01And, you know, I routinely ingest a million rows with Polars in a sub-second.
- 45:07So I've yet to need concurrency because the size of the data stuff that I deal with isn't in that petabyte range where it actually would start to matter.
- 45:16So there are mechanisms there, but I can't speak to them very well.
- 45:19Yeah, I haven't done them either.
- 45:22So time to move on?
- 45:24Yes, time to move on.
- 45:24Apparently, yeah, apparently there are ways to do it, but it's not worth going over in audio.
- 45:29Okay, let's move on.
- 45:32xdt so sorry yeah i was i wasn't being snarky there i was no no i know
- 45:41of course this is a date time functionality library uh and it actually comes from the
- 45:47polars folks so i think what they're kind of doing here is kind of like that contrib thing
- 45:51inside of django which is yeah we don't really need this we'll keep it separate but if you need
- 45:56it's there it essentially is a collection i think it's like a 10 or 12 uh functions that essentially
- 46:02are extra date stuff for example last one was um last commit was by marco corelli who's been on the
- 46:08show before yeah there you go uh so for example the date range function returns a series of dates
- 46:14and can be used to populate a column in data frame so it takes a start and end date as arguments and
- 46:19then will work with a variety of intervals and allows you to ignore weekends and holidays so
- 46:24if you want to generate like a time series kind of thing, this will do that. The day name function
- 46:30returns the name of the day of the week, supporting localization. Similarly, there's format localized,
- 46:36and I think there's other stuff for months and things like that in here as well. The one I kind
- 46:40of liked, I've done a couple courses on astronomy because a colleague of mine is working on his
- 46:47degree in this space. And so it always fascinates me when you move sort of sideways in a science and
- 46:54you're doing what? So this library supports calculating a Julian date, which is kind of
- 47:00like a Unix epoch, except day one is in 4713 BCE. And although this is fun for history buffs,
- 47:09it gets used in astronomy so that they can do time lapse calculations as simple addition and
- 47:14subtraction. And so it's like Julian one versus whatever we're in now, which is Julian 6000
- 47:20in something. And so if you're as strange as this may seem, if you happen to be working in that space,
- 47:27this plugin allows you to switch between Julian and back and forth. And then of course, Julian
- 47:33dates also mean something completely different. So that gets confusing as well, but you know,
- 47:37welcome to software. And welcome to date times and software for sure. Yes. We have double so in
- 47:43date time. Yeah. So there's some interesting things here and I have some real time follow
- 47:46up here in a second. So it says blazing fast written in Rust convert to and from multiple
- 47:50time zones, which is great, format date time zones and different locales, dueling dates. And then
- 47:56there's a couple of things that are crossed off and say they've been upstream to polars, which is
- 48:00really cool. It's like, actually, this turns out to be important enough. It should just be in polars.
- 48:05So there's this cool interplay here for sure. Yeah, I'm pretty sure this is somebody's playground.
- 48:10So I think they're maintaining it separately. And I think if it's something that's popular enough,
- 48:14it'll move up but uh and it is in the GitHub sorry it is in the GitHub uh polars repos not
- 48:21the organization not just some yeah yeah and that was kind of what i was about to say right back to
- 48:25your question about sort of a trust thing well the people who write the library when they write
- 48:29another library to go with it you're probably okay absolutely um marco garelli this my real
- 48:35time follow-up says hey some of these didn't fit into polars because they would have increased the
- 48:40size of the binary too much due to extra dependencies right so that's great like too
- 48:45heavy weight but let's make it an extra so yeah pretty cool that's a good reason to do it yeah
- 48:50yeah indeed all right let's go and talk statistics a bit yeah what's the chances of that huh so so
- 48:57from making that from from simple math to harder math yeah uh so let's say you're doing some audio
- 49:05signal processing and you need to pull some of the frequencies out or you want to try and remove some
- 49:11noise this process is called filtering one way of doing this is to use something called a least mean
- 49:17square filter which is a special calculation that tries to minimize the mean square of the error
- 49:23between the signal and the desired result you too can sound like you know what you're talking about
- 49:28by reading a wikipedia entry into a microphone so uh if most of what i said was meaningful to you
- 49:34then the Polars OLS library might be your thing.
- 49:38This is by Azmi Rajab
- 49:40and performs least mean squares calculations
- 49:43using a variety of different mechanisms.
- 49:45Allows you to do this on at,
- 49:47it allows you to do this as a Polars expression.
- 49:49And of course the underlying code,
- 49:51like a lot of these are as Rust based.
- 49:53So it's nice and snappy performance wise.
- 49:55The expression even includes different ways
- 49:57of dealing with nulls,
- 49:58which is one of those things
- 49:59that gets in the way with real world data.
- 50:02So this library strikes me as one of those things
- 50:04will save someone a whole lot of time so if you happen to be that someone there you go yeah it's
- 50:10excellent i you know i think in data science there's a lot of this tension of do i actually
- 50:16need to know the algorithm and the details of this or do i just need to know that i can call
- 50:20this function and when i should and when i should call it and i think that's a valid tension right
- 50:26because if something you call it on the wrong data and you get the wrong answer but you're like
- 50:29look i've discovered life on another planet oh no i discovered dust or whatever right you know it's
- 50:36um i was uh doing a bit of tutoring for a friend of mine's daughter a few years back and uh one of
- 50:43the things i ran into is you know you're allowed to use calculators now which when i was a kid
- 50:48calculators basically didn't exist and you weren't allowed to touch them but now this is part of the
- 50:52math program um and one of the things we were talking about a lot is well if you don't know
- 50:56what answer you're expecting, then when it says a thousand and it was supposed to be
- 51:01C10, you're not going to know you hit the wrong button.
- 51:03So I think that's kind of that.
- 51:04This is exactly what you're talking about.
- 51:06You need to understand it enough to sort of go, am I getting something back that makes
- 51:10sense?
- 51:11But if you just call the function blindly, you know, garbage in, garbage out.
- 51:16But if you, you know, there's a lot of depth to some of these things and you could spend
- 51:21an entire career trying to understand it all and that's not going to help you solve the
- 51:24problem you're trying to solve, right?
- 51:26So yeah, like you said, balance.
- 51:28Indeed.
- 51:28What about pairing?
- 51:30Okay, so this one's off the charts.
- 51:37So a pairing function is one that takes two natural numbers
- 51:42and maps that to a single natural number.
- 51:45This process can also be undone.
- 51:47So you can take a single natural number
- 51:48and find out what pairs could be used to create it.
- 51:51This is related to set theory and gets into that funky domain
- 51:55where you can start to talk about some kinds of infinity
- 51:57are bigger than other kinds of infinity.
- 52:00The Polars Pairing Library by Antonio Camargo
- 52:03provides three different pairing functions.
- 52:06I'm not even going to try.
- 52:07One of which is Cantor, that one I can pronounce,
- 52:10which allows you to pair columns in a data frame
- 52:13or unpair results.
- 52:15I've got the Sudzik and Hagen as well.
- 52:18Got them all.
- 52:18There you go.
- 52:19If you Google around, you can find all sorts of articles
- 52:22on what this is and how it gets used in set theory.
- 52:25how you'd use it in your code is a mystery to me but maybe i just didn't dig enough
- 52:31uh but if uh if you're mucking around and trying to understand canter pairing why not uh if on the
- 52:38other hand one of the things i saw come up a couple times with that some of these libraries are often
- 52:43used as examples for how to write a polars library uh so if you there are certain things you need to
- 52:49do if you're writing a plugin and so some of these small libraries as much as this might not apply to
- 52:54your day-to-day, because they're a small library, they can actually be helpful for you to kind of
- 52:59understand the structure of things because you don't need to understand the math underneath this.
- 53:03That's just a function call, but you can see the structure of things out there. So sometimes
- 53:07something like this that is, why would I do that, could actually be a good lesson.
- 53:12Yeah. Okay. Well, it seems like maybe encryption. I don't know. But yeah,
- 53:17if you're doing set theory. In theory, this has to do with
- 53:20hashing in practice i think there are better ways of hashing so um yeah yeah you read it how about
- 53:26that yeah all right so as a complete aside um there's a several list items on the polar's
- 53:33package list use the little bear emoji in their docs and i'm not sure if this is just my font
- 53:38but it looks like a panda and it looks like a panda to me too yeah so so to be clear if polar
- 53:43bears and panda bears lived in the same place panda bears would be appetizers and they even
- 53:47come with their own little bamboo garnish. So funnily enough, I'm not sure if this is a strong
- 53:53signal, but not one of the ones that used the barren emoji was one I could get working on my
- 53:58system. So I don't know, or whatever that's worth. Anyways, so we kind of got into the weeds there.
- 54:05So now we'll like step back a little bit and talk about a few things that are a little more higher
- 54:10level. The Polars List Utils, a good name, kind of describes what it does. This is by Travis
- 54:17Hammond and it gives you a series of functions that operate on columns that have lists within
- 54:22the column. So for example, one of the stronger uses here is something called aggregate list
- 54:28column element wise, which is a long function name, but it does aggregation operations on the values
- 54:34inside the list data, supporting the same kind of aggregation that regular Polars does. So you
- 54:39can do a mean account or some or whatever on those things that are in the column. So the library seems
- 54:46to be coming out of the signal processing space, I suspect, because it supports a whole bunch of
- 54:52fast Fourier transform mechanisms, including applying them, getting frequency bins, linear
- 54:59spacing, and all that kind of stuff. The library also has some decent examples in its repo. This
- 55:04was obviously written by somebody doing something with audio, because there's a whole bunch of sample
- 55:09data and graph pictures and things like that on it. As a quick aside, this project explicitly
- 55:16required Python 3.10 through 3.12, but I was able to get it working using the ignore requires Python
- 55:22flag to pip. So I don't think there's anything, I was using 3.13 and there wasn't anything that
- 55:27was actually 3.12 specific. So if you bump into that, if you're playing with it, don't worry about
- 55:32that too much. Interesting. Yeah, probably they just over-constrained their requirements when
- 55:37specifying the package, right? I ran into it with a couple of the other libraries that I tried out
- 55:41and they actually didn't work uh this wasn't that case though so this worked fine okay yeah very
- 55:47interesting and then i hardly believe it i hardly believe it uh i love the image with this one
- 55:52the nice biker going thing here uh so this is harley this is by tom burge and although the docs
- 56:00explicitly say it isn't associated with the bikes or the comic there's a nice picture of a motorcycle
- 56:05in acknowledgement uh and an acknowledgement that this is a port of a pie spark library called quinn
- 56:11where it comes from. This is a catch-all library. It's got a bunch of different things. So for
- 56:19example, anti-trim, remove all white space, and single space are all functions that deal with
- 56:25leading, trailing, multi-white space inside of strings. Approximate equal compares two columns
- 56:32for equality within a given threshold. So that's kind of useful if you've got a couple floats and
- 56:36And, you know, if it's within 10% of each other, then that's good enough.
- 56:40Multi equals checks if multiple columns have a specified value.
- 56:44So, you know, again, different comparison things.
- 56:47Div or else divides one column by another, but allows for a default value if the divisor is zero.
- 56:53So that's useful when you're not supposed to be dividing by zero.
- 56:56I'm told that's not allowed.
- 56:59And then there's some validation tools.
- 57:00It's just a good recommendation.
- 57:02Yeah.
- 57:03And there's also some validation tools, Boolean tools, and a few things in here that can help you get information about your schema.
- 57:10So nice little all told collection.
- 57:13And I love these kinds of things.
- 57:16There's a couple of ones in the Django world that there's one I wrote and there's the extensions one as well.
- 57:22These collections are often I find these are the ones that often save me the most amount of time because there's usually something in them that I'm going to use again.
- 57:30because this is, you know, it's written by somebody who was trying to solve some problem
- 57:34and just didn't want to do it twice.
- 57:36Yeah, very cool.
- 57:37Yeah, I think, yeah, I think that might be all over.
- 57:39That's our list.
- 57:41That's the valid list, yep.
- 57:42And I was thinking after we went through them, I might know if it were 10 or not,
- 57:46but I'm even more confused because we went down so many tangents.
- 57:49I don't even know whether some of them count.
- 57:51But I can tell you that those are a lot of helpful tools and libraries for people.
- 57:56So make sure you're putting that list together and doing all the research, Chris.
- 57:59Yeah, for sure.
- 57:59And, you know, we only really covered a few of the plugins.
- 58:03The Awesome Polars list also has links to articles, links to blog posts, things on different interfaces for Polars on other languages that you can use to interact with it.
- 58:18So even over and above the libraries we talked about there, there's a fair amount of decent content there.
- 58:24Yeah, absolutely.
- 58:25All right.
- 58:26A couple of final calls to action.
- 58:27I'll let you have the final word but I'll do a few for you check out your Django book very cool
- 58:33even has some HTMX in it check out your Polars course which I'll link to in the show notes and
- 58:38is very germane to this conversation and with that people want to do some Polars maybe want to
- 58:45level up a little bit with some of these things we talked about what do you tell them um you know
- 58:49it's a very approachable library I think it's uh it's one of those things you can dig in fairly
- 58:53easily to um not to say hey you know the course is very helpful but the guides that come with
- 59:01pullers are also very very readable as well um so if you're not if you if you're saving up to take
- 59:07the course later hit the guides up as well they can be helpful uh and the community seems to be
- 59:12pretty good i've seen a fair amount of your typical python help each other out kind of space as well
- 59:17so yeah it's a fun little library and uh very very useful for solving some of your data science
- 59:23Thanks.
- 59:24Awesome.
- 59:25Thank you.
- 59:25Let me do one more quick little shout out
- 59:27now that I'm thinking about it at the end here.
- 59:29Speaking of Marco Garelli,
- 59:30I had him on recently to talk about Narwhals,
- 59:33which is a library that will help you bridge code
- 59:36between working with pandas
- 59:37and working with Polars and other data frame libraries.
- 59:40So if you've got some code
- 59:41and you want to try out Polars on it,
- 59:43but it's mostly in pandas
- 59:44or some other data frame library,
- 59:46like, I don't know, Dask or something,
- 59:48you can check out Narwhals as well.
- 59:51And the episode is 480.
- 59:53so not too long ago.
- 59:54Anyway, one more thing,
- 59:55people to help them get started.
- 59:57All right, Christopher,
- 59:58thanks for being on the show.
- 59:59Always fun to catch up with you.
- 1:00:01Yeah, great.
- 1:00:02Thanks for having me.
- 1:00:02Yeah, bye.
- 1:00:04This has been another episode
- 1:00:05of Talk Python To Me.
- 1:00:07Thank you to our sponsors.
- 1:00:09Be sure to check out
- 1:00:10what they're offering.
- 1:00:10It really helps support the show.
- 1:00:12Take some stress out of your life.
- 1:00:14Get notified immediately
- 1:00:15about errors and performance issues
- 1:00:17in your web or mobile applications
- 1:00:19with Sentry.
- 1:00:20Just visit talkpython.fm
- 1:00:22slash Sentry and get started for free. And be sure to use the promo code Talk Python, all one word.
- 1:00:29And it's brought to you by Agency. Discover agentic AI with Agency. Their layer lets agents find,
- 1:00:35connect, and work together, any stack, anywhere. Start building the internet of agents at
- 1:00:40Talk Python.fm slash agency, spelled A-G-N-T-C-Y. Want to level up your Python? We have one of the
- 1:00:47largest catalogs of Python video courses over at Talk Python. Our content ranges from true
- 1:00:52to deeply advanced topics like memory and async.
- 1:00:55And best of all, there's not a subscription in sight.
- 1:00:58Check it out for yourself at training.talkpython.fm.
- 1:01:01Be sure to subscribe to the show,
- 1:01:03open your favorite podcast app,
- 1:01:04and search for Python.
- 1:01:06We should be right at the top.
- 1:01:07You can also find the iTunes feed at /itunes,
- 1:01:10the Google Play feed at /play,
- 1:01:12and the direct RSS feed at /rss on talkpython.fm.
- 1:01:16We're live streaming most of our recordings these days.
- 1:01:19If you want to be part of the show
- 1:01:20and have your comments featured on the air,
- 1:01:22be sure to subscribe to our YouTube channel
- 1:01:24at talkpython.fm/youtube.
- 1:01:28This is your host, Michael Kennedy.
- 1:01:29Thanks so much for listening.
- 1:01:30I really appreciate it.
- 1:01:31Now get out there and write some Python code.
- 1:01:58I think is the norm.