Practical Business Python

Taking care of business, one python script at a time

Newsletter Number 1

Sent on Mon 16 July 2018

Welcome to the first edition of the Practical Business Python newsletter!

While attending PyCon in Cleveland, I had the chance to meet a lot of fellow python content creators who encouraged me to start this list. They all said that an email list is one of the best ways to engage with an audience. It’s taken longer than I hoped but I’m excited to start this journey with all of you.

My goal is to use this newsletter as a way to share content that might not make a full blog post but is more substantial than a tweet. I am also very interested in hearing your feedback so please let me know what works and does not for you. I am most looking forward to learning about topics that might make good blog posts in the future. Also, please feel free to forward on this email to others that might find this content interesting.

PB Python Articles

Here are a few updates on things going on with the blog:

  • There is a good discussion on formatting Outlook emails in the Automating Windows Applications with COM article. Feel free to comment if you are doing any other interesting things with Windows automation.
  • I am somewhat surprised that one of the most popular searched topics that leads to the blog is the article on Creating Pandas Dataframes from Lists and Dictionaries. I recently found out that pandas from_items() function has been deprecated. You can use the following (somewhat clunky) code instead: pd.Dataframe.from_dict(dict(sales)).

Other Project Updates

There are so many other exciting python projects in the python ecosystem. Here are a couple recent updates that I’ve been paying attention to:

  • Bokeh recently released version 0.13. I have had a chance to play around with the new range selection tool and it is pretty handy. I also think the new and improved template support will be really useful. I will try to write up a more detailed post once 1.0 hits.
  • As I was about to finalize this email, I saw that Seaborn released version 0.9. Check out the Changelog to see what is in store. There are some fairly big changes to the API which I think will be good but mean we will need to alter our Seaborn code in future releases as functions are deprecated. There may be some short term adjustments but long term I think these are great changes for the Seaborn library. I will try to write a post once I get a chance to use it some more.
  • Pandas released version 0.23.2 then 23.3.3 and it includes a handful of bug fixes and minor improvements. I like to stay up to date with the current pandas version as much as feasible. I’ve definitely had regressions pop up when upgrading in the past. These updates seemed to be seamless for me.
  • Packaging your python project can be a bit mysterious. I’ll be honest, I should probably spend some more time figuring out a good way to distribute my python scripts at work. When I do, I’ll definitely use this guide .

Code Snippets

I recently was aggregating some monthly sales data at the quarterly level. I originally tried using Pd.Grouper() with some code that looks like this:

df.groupby(['Product', pd.Grouper(key=('Date'), freq='Q')])

but did not like the way the end results labeled the Quarters as : 2017-12-31 or 2018-3-31

It think it is confusing for the end user. Why have a specific date when it really represents a time range?

After some searching, I found a nice approach using PeriodIndex which will return values using the format 2017Q4 or 2018Q1 which makes much more sense from an end user perspective. This code applies the PeriodIndex to the Date column and returns the quarterly label based on the specific date. It’s easy to aggregate at the quarterly level in a very intuitive manner.

df['Qtr'] = pd.PeriodIndex(df['Date'], freq='Q')

df.groupby(['Product', 'Qtr']).sum()

In my experience this example is going to be more easily understood by the end users of the report. It also shows yet another way that pandas’ rich API for manipulating data can be confusing to navigate but is worth the effort once it is mastered.

Wrapping Things Up

Well, that’s the end of the first newsletter. I hope you find it useful and are looking forward to the next one. Please let me know what you think and let me know what types of topics you might be interested in seeing in future blog posts!