select from timestamp column where year greater than 2024
- User
select from timestamp column where year greater than 2024
Assistant
To select rows from a Parquet file where a
timestampcolumn's year is greater than 2024, you can use DuckDB's SQL functions to extract the year from a timestamp column.Here’s how to do it in Python using DuckDB:
✅ Example: Filter by Year from a Timestamp Column
import duckdb # Connect to an in-memory DuckDB instance conn = duckdb.connect() # Query: Select rows where the year of 'timestamp_column' is > 2024 query = """ SELECT * FROM 'path/to/your/file.parquet' WHERE EXTRACT(YEAR FROM timestamp_column) > 2024; """ # Execute and fetch the result result_df = conn.execute(query).df() # Print the filtered DataFrame print(result_df)
🔍 Notes:
- Replace
'path/to/your/file.parquet'with the actual path to your Parquet file. - Replace
timestamp_columnwith the actual name of your timestamp column. - DuckDB supports timestamp functions like
EXTRACT(YEAR FROM ...),DATE_PART(...), etc.
🚀 Alternative: Using
read_parquetwith Filters (Pushdown)If you want to use DuckDB’s filter pushdown for performance (especially for large files), you can use DuckDB's internal functions like
year():filters = [ ('timestamp_column', '>=', '2025-01-01') # More efficient than EXTRACT ] df = duckdb.read_parquet('path/to/your/file.parquet', filters=filters).to_df() print(df)💡 Tip: Using range-based filters like
timestamp_column >= '2025-01-01'is often faster and more efficient than usingEXTRACT(YEAR FROM ...), because it can utilize Parquet's statistics and avoids full scans.
Let me know if you want to apply this across multiple Parquet files or in a partitioned dataset.
- Replace
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.