79073313

Date: 2024-10-10 08:05:18
Score: 1
Natty:
Report link

Despite the fact that @MaKruQ gave a good answer and @Johnny Cheesecutter gave a good link, I still received an error for various tables related to the incompatibility of Excel and dates with the time zone. So I decided to rewrite the function as follows and it helped me.

def save_to_file(
    filepath: str,
    date_columns: str | List[str] = None  # add new optional parameter to specify column with object dtype and datetime\Timestamp type into it
) -> Callable[
    [Callable[F_Spec, F_Return]],
    Callable[F_Spec, F_Return]
]:
            def convert_timezones(df: DataFrame) -> DataFrame:
                def convert(value):  # add function to check and replace tzinfo
                    if isinstance(value, Timestamp) and value.tz is not None:
                        return value.tz_convert(None)
                    elif isinstance(value, datetime):
                        return value.replace(tzinfo=None)
                    return value

                for column in df.select_dtypes(include=[
                    'datetime64[ns, UTC]',
                ]) + df[date_columns]:
                    df[column] = df[column].apply(convert)
                return df

It should be noted that my data had columns that contained None, and a date in a row, and a timestamp, and a datetime, and even a date like 0001-01-01 00:00:00+00:00. Changing the function solved the problem. But I still don't like to pass the date myself... although it's better than just looping through all the columns in the dataframe

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @MaKruQ
  • User mentioned (0): @Johnny
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Sergey Bakaev Rettley