Skip to content

Instantly share code, notes, and snippets.

@joreilly86
Created November 1, 2024 19:17
Show Gist options
  • Select an option

  • Save joreilly86/d5bcf5f9e71ee165f11820571246802a to your computer and use it in GitHub Desktop.

Select an option

Save joreilly86/d5bcf5f9e71ee165f11820571246802a to your computer and use it in GitHub Desktop.
This code supports Flocode Newsletter #052 - Python Dictionaries for Engineers
# This code supports #052 - Python Dictionaries for Engineers
# It demonstrates the use of dictionaries, Pandas, and Polars for engineering data handling
import pandas as pd
import polars as pl
from datetime import datetime, timedelta
import random
# Dictionary storing material attributes
steel = {
'Youngs Modulus': 210e9, # Pa
'Density': 7850, # kg/m^3
'Tensile Strength': 400e6 # Pa
}
# Dictionary storing loads for structural components
loads = {
'Column1': {'Dead Load': 1200, 'Live Load': 500},
'Beam2': {'Dead Load': 1000, 'Live Load': 450}
}
# Load case data dictionary
load_cases = {
'Dead Load': [1200, 1300, 1250],
'Live Load': [500, 600, 550],
'Wind Load': [300, 350, 325]
}
# Convert dictionary to Pandas DataFrame
df_pandas = pd.DataFrame(load_cases)
print("Pandas DataFrame:")
print(df_pandas)
print()
# Convert dictionary to Polars DataFrame
df_polars = pl.DataFrame(load_cases)
print("Polars DataFrame:")
print(df_polars)
print()
# Summing each column in Pandas
load_summary_pandas = df_pandas.sum()
print("Pandas Sum:")
print(load_summary_pandas)
print()
# Summing each column in Polars
load_summary_polars = df_polars.select(pl.all().sum())
print("Polars Sum:")
print(load_summary_polars)
print()
# Filtering in Pandas
filtered_loads_pandas = df_pandas[df_pandas['Dead Load'] > 1200]
print("Pandas Filtered:")
print(filtered_loads_pandas)
print()
# Filtering in Polars
filtered_loads_polars = df_polars.filter(pl.col('Dead Load') > 1200)
print("Polars Filtered:")
print(filtered_loads_polars)
print()
# Generate synthetic data
start_date = datetime(2022, 12, 25)
end_date = datetime(2023, 3, 1)
sensor_ids = ['S001', 'S002', 'S003', 'S004', 'S005']
data = []
current_date = start_date
while current_date <= end_date:
for sensor_id in sensor_ids:
reading = random.uniform(20, 100) # Random reading between 20 and 100
data.append({
'timestamp': current_date.strftime('%Y-%m-%d %H:%M:%S'),
'sensor_id': sensor_id,
'reading': round(reading, 2)
})
current_date += timedelta(hours=1)
# Create a Polars DataFrame
df = pl.DataFrame(data)
# Print first few rows to verify
print("Synthetic Data Sample:")
print(df.head())
print()
# Demonstrate Polars' lazy evaluation
result = (
df.lazy()
.filter(pl.col("timestamp") > "2023-01-01")
.group_by("sensor_id")
.agg([
pl.col("reading").mean().alias("avg_reading"),
pl.col("reading").max().alias("max_reading")
])
.sort("avg_reading", descending=True)
.limit(10)
)
print("Polars Lazy Evaluation Result:")
print(result.collect())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment