Skip to main content

Python examples

Use Magic to help with all of your Pythonic workflows

Pandas dataframe operations

Magic is excellent at everyone's least favorite thing: complex dataframe operations.

  • Long chained filters and aggregations:
  • Refactoring many operations into a one-liner:
  • Refactoring a confusing one-liner into more readable, documented components:
  • Parsing misformatted timestamps: This prompt is a great example of the 'provide a data example' technique described in General tips.

Exploratory data analysis

  • Dig into a specific ad-hoc question:

Use libraries without needing documentation

Often Magic can come up with necessary libraries on its own, but you can also specify which libraries you want it to use for a task and it'll import them if necessary.

  • Geocode data and build an interactive map: To make this by hand, chances are you'd have to refer to the documentation for geopy and pydeck to get everything just right.
  • Time series forecasting with Prophet: Prophet requires data to be in a particular format, and for a special future dataframe to be created, both of which Magic does automatically.
  • Charting with matplotlib: matplotlib can be notoriously verbose when building complex charts. Magic takes care of all of the boilerplate.
  • Tweaking chart layouts: Often something is just a tiny bit off, and Magic can easily rebuild charts (or any code) to a different spec.

  • Web scraping with beautifulsoup: This is tougher, but Magic can often correctly discover and scrape down data from webpages. Try providing your own url and being specific about how the data is stored on the page to improve results.

Converting and refactoring code

  • Converting a chart from matplotlib to plotly:
  • Switching between libraries without Googling: Here we switch from a free rate-limited API to an unlimited one, without having to look up and rewrite new syntax.
  • Improving performance of slow code: In this specific case, Magic sped up the code 2.4x by doing expensive processing outside of a nested for loop.

Complete analyses

Magic is optimized to return code that helps you do your work— not to actually do the work for you. However, it can still be extremely useful for answering bigger questions in just one prompt, or doing iterative analysis with you.

  • Summarize seasonality:
  • Ask iterative questions to unpack a dataset: Protip: use generate instead of edit when doing iterative analysis with Magic to preserve your previous work.

On this page