How to Analyze Time Series Data with Lux

How to Analyze Time Series Data with Lux
Image by Editor | ChatGPT

Time series data presents unique visualization challenges. Effective temporal analysis requires showing trends, seasonality, and patterns across different time scales. Lux simplifies this process by automatically detecting datetime columns and suggesting relevant temporal visualizations.

Understanding Lux Data Types

Lux assigns semantic data types to each column in your DataFrame, which determine how the data will be visualized. These data types are different from pandas’ dtypes and provide higher-level information about the role of each attribute.

The main data types supported by Lux include:

  • Quantitative: Numerical measures (e.g., counts, measurements)
  • Nominal: Categorical data with no inherent order
  • Temporal: Date and time information
  • Geographic: Location data like countries or states
  • ID: Identifier columns that shouldn’t be visualized

For more details on these supported data types, I recommend visiting the official Lux documentation that covers this topic. 

Let’s see how Lux automatically detects data types in the flights dataset:

 
import pandas as pd
import seaborn as sns
import lux

# Load the flights dataset
flights = sns.load_dataset('flights')

# Display the data types
flights.data_type

When you run this code, you’ll see both a warning and the detected data types:

 
/opt/anaconda3/lib/python3.8/site-packages/lux/executor/PandasExecutor.py:448: UserWarning:
Lux detects that attributes ['year', 'month'] may be temporal.
To display visualizations for these attributes accurately, please convert temporal attributes to Datetime objects.
For example, you can convert a Year attribute (e.g., 1998, 1971, 1982) using pd.to_datetime by specifying the `format` as '%Y'.

Here is a starter template that you can use for converting the temporal fields:
	df['year'] = pd.to_datetime(df['year'], format='')
	df['month'] = pd.to_datetime(df['month'], format='')

See more at: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html
If month is not a temporal attribute, please use override Lux's automatically detected type:
	df.set_data_type({'month':'quantitative'})
 
{'year': 'temporal', 'month': 'temporal', 'passengers': 'quantitative'}

Lux has correctly identified year and month as temporal attributes and passengers as quantitative. However, the warning informs us that we should convert these temporal columns to proper datetime objects for better visualization. This is a common step when working with time series data in Lux.

Setting Up Time Series Data

When working with time series data in Lux, it’s necessary to properly prepare your data to ensure accurate and meaningful visualizations. Depending on your dataset, this may involve several steps: 

 
# Step 1: Create a proper date column by combining year and month
flights['month'] = flights['month'].astype(str)
flights['date'] = pd.to_datetime(flights['year'].astype(str) + '-' + flights['month'], format='%Y-%b')

# Step 2: Override data types for year and month
# This tells Lux that we don't want to treat year and month as temporal attributes
flights.set_data_type({'year': 'quantitative', 'month': 'nominal'})

# Check the data types again
flights.data_type

After running this code, you’ll see that Lux now recognizes our new structure:

 
{'year': 'quantitative',
 'month': 'nominal',
 'passengers': 'quantitative',
 'date': 'temporal'}

These preprocessing steps are essential because:

  1. We create a proper datetime column (date) that Lux can use for time-based visualizations
  2. We specify that year should be treated as a number and month as a category
  3. This combination prevents warnings and ensures our visualizations work correctly

Creating More Insightful Time Series Visualizations

Now that our data is properly prepared, we can tell Lux what we want to analyze by expressing our intent:

 
# Express intent to analyze passenger counts over time
flights.intent = ["date", "passengers"]

# Display the dataframe with this intent
flights

When you toggle to the Lux view, you’ll see an informative line chart showing passenger trends over time:

Line and grouped bar charts showing airline passengers over time, broken down by month.
Image By Author
 
Looking at the visualizations, we can immediately spot interesting patterns:

  • A clear upward trend in passenger numbers from 1949 to 1960
  • Strong seasonal fluctuations with peaks occurring at regular intervals
  • The summer months appear to have consistently higher passenger numbers

Exploring Seasonal Patterns

To investigate the seasonal patterns more clearly, we can create a season column and include it in our visualization intent:

 
# Create a season column for clearer comparisons
flights['season'] = flights['month'].apply(lambda x: 
    'Summer' if x in ['Jun', 'Jul', 'Aug'] else
    'Winter' if x in ['Dec', 'Jan', 'Feb'] else
    'Spring' if x in ['Mar', 'Apr', 'May'] else 'Fall')

# View seasonal patterns
flights.intent = ["date", "passengers", "season"]
flights

This visualization clearly shows the seasonal patterns:

Grouped bar chart showing airline passengers over time, categorized by season.
Image By Author
 

We can now see that:

  • Summer consistently has the highest passenger numbers throughout the period
  • Winter generally has the lowest passenger numbers
  • The gap between seasons grows larger over time, suggesting seasonal travel became more pronounced
  • All seasons show an upward trend, indicating overall industry growth

Practical Time Series Tips

When working with time series data in Lux, keep these points in mind:

  1. Convert categorical time attributes: Month names or year numbers often need conversion to proper datetime objects or explicit data type overrides.
  2. Combine date components: When your data has separate year, month, or day columns, combine them into a single datetime column for better visualizations.
  3. Use data types strategically: Setting temporal columns as quantitative or nominal can provide different analytical perspectives.
  4. Create meaningful groupings: As shown with our seasons example, grouping time periods can reveal patterns that might be hidden in more granular views.

For additional examples and tips, please refer to the official Lux documentation on how to work with temporal datetime columns. 

Conclusion

Time series analysis with Lux not only saves time but also encourages exploration of different temporal perspectives. By providing automated recommendations and allowing for simple intent expressions, Lux helps you discover insights that might otherwise remain hidden in your data. Whether you’re analyzing business metrics, financial data, or any other time-dependent information, the combination of Lux’s automated recommendations and customizable visualizations makes it an excellent tool for rapid and insightful temporal data exploration.

Leave a Reply

Your email address will not be published. Required fields are marked *