A package to automatically standardize dates in Pandas DataFrames.
Project description
df-datefixer
A lightweight Python library to standardize date columns in Pandas DataFrames. It automatically handles multiple date formats, missing values, problematic entries, multi-column fixing, optional datetime conversion, and more.
See the package on PyPI.
See the package on GitHub
Installation
Install via pip:
pip install df-datefixer
Usage
Basic usage:
import pandas as pd
from df_datefixer.fixer import fix_dates
df = pd.DataFrame({
'event_date': ['2022-01-01', '1/2/2022', 'bad-date', None]
})
fixed_df = fix_dates(df, column="event_date", target_format="%Y-%m-%d", missing_value="0")
print(fixed_df)
Multiple columns, datetime conversion, and custom placeholders:
df = pd.DataFrame({
'start_date': ['2022-01-01', '1/2/2022', 'bad-date', None],
'end_date': ['2022-02-01', 'invalid', '03-03-2022', None]
})
fixed_df = fix_dates(df, columns=['start_date', 'end_date'], convert_to_datetime=True, missing_value="NaT")
print(fixed_df)
Important note about datetime conversion:
If you set convert_to_datetime=True with a custom missing_value, your column might be converted to object type instead of datetime, because custom placeholders might not be datetime-compatible. For pure datetime operations, leaving missing_value as "NaT" (default datetime placeholder) is recommended.
Parameters
df: A pandas DataFramecolumn: Column name containing dates (single column)columns: List of column names to fix simultaneouslytarget_format: Desired standardized date format (default is "%Y-%m-%d")missing_value: Replacement for missing/unparsable dates (default is "0")verbose: Print details about problematic dates (default isTrue)convert_to_datetime: Converts fixed dates to pandas datetime type ifTrue(default isFalse)inplace: Modifies DataFrame in place ifTrue, else returns a new DataFrame (default isFalse)
Development
Set up virtual environment
python -m venv venv
source venv/bin/activate # on macOS/Linux
.\venv\Scripts\activate # on Windows
Clone repository and install dependencies
git clone https://github.com/kyriaki-mvr/df-datefixer.git
cd df-datefixer
pip install -e .
pip install pytest
Run tests
pytest tests
Contributing
Contributions are welcome! If you'd like to contribute, please fork the repository and use a feature branch. Pull requests are warmly welcomed.
Feedback
If you have any feedback, please file an issue on the GitHub repository page. I am always looking to improve and appreciate all feedback, bug reports, and suggestions.
Authors
Kyriaki Mavropoulou (kyriaki@quanterra.gr) - Initial work
License
df-datefixer is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file df_datefixer-1.0.2.tar.gz.
File metadata
- Download URL: df_datefixer-1.0.2.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
31abd94a32e6eb7cc0310319c9cda0da329b97925dfbdf7a0daaa1ec0738ce0a
|
|
| MD5 |
eb0bab5fd31173cf81fd9911f7070fcc
|
|
| BLAKE2b-256 |
a981b5269e11c9d24149affa9c695071658cdfbd1fd9f49d947a939a8f6f2016
|
File details
Details for the file df_datefixer-1.0.2-py3-none-any.whl.
File metadata
- Download URL: df_datefixer-1.0.2-py3-none-any.whl
- Upload date:
- Size: 3.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fcc159e31baf89c830c3c805dc5c4841b2d7926e825c6c772627aa76a68727c3
|
|
| MD5 |
ebf62f33b105f979eddca5c3e71e2f7f
|
|
| BLAKE2b-256 |
ab2af573ec557cd59222dbd9f174a594f7ecb4fd481ed13c975980261d47eb0b
|