Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Gabriel0110
Copy link
Contributor

Implement check and handling for timezone-aware datetime objects, converting them to timezone-naive for the .astype() numpy conversion function to work properly and not throw a TypeError if the dataframe has any timezone-aware datetime objects.

The issue is attempted conversion via numpy's .astype() function via df[col[0]] = df[col[0]].astype(data_type) with "datetime64[ns]" as the data type on datetime objects with a timezone.

Numpy will throw the below error if there are any timezone-aware datetime objects in the dataframe:

TypeError: Cannot use .astype to convert from timezone-aware dtype to timezone-naive dtype. Use obj.tz_localize(None) or obj.tz_convert('UTC').tz_localize(None) instead.

The proposed fix allows the dataframe to have timezone-aware datetime objects and still use skimpy to visualize the data analysis.

This can be tested with dummy data such as below:

import pandas as pd
from datetime import datetime
import pytz

# Sample data
data = {
    "timezone_aware": [pd.Timestamp(datetime.now(), tz=pytz.UTC)],
    "timezone_naive": [pd.Timestamp(datetime.now())],
    "integer_col": [1],
}

df = pd.DataFrame(data)

skim(df)

Gabriel0110 and others added 2 commits December 13, 2024 15:44
…verting them to timezone-naive for the .astype() numpy conversion function to work properly and not throw a TypeError if the dataframe has any timezone-aware datetime objects.
@aeturrell aeturrell added this to the 0.0.16 milestone Dec 15, 2024
@aeturrell aeturrell merged commit 38b5e5a into aeturrell:main Dec 15, 2024
11 of 12 checks passed
@aeturrell
Copy link
Owner

Thank you for contributing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants