Change timezone info for multiple datetime columns in pandas

2024/10/17 20:52:31

Is there a easy way of converting all timestamp columns in a dataframe to local/any timezone?

Not by doing it column by column?

Answer

You can selectively apply the conversion to all datetime columns. First, select them with select_dtypes, then call tz_convert inside apply:

df2 = df.select_dtypes('datetimetz')
df[df2.columns] = df2.apply(lambda x: x.dt.tz_convert(None))

"datetimetz" selects all datetime columns with timezone information.
Replace tz_convert(None) with tz_convert(your_timezone) to convert to a different timezone.

For plain datetime objects without any timezone info, call tz_localize first with your current timezone before calling tz_convert.

https://en.xdnf.cn/q/72976.html

Related Q&A

Change permissions via ftp in python

Im using python with ftplib to upload images to a folder on my raspberryPi located in /var/www. Everything is working fine except that uploaded files have 600 permissions and I need 644 for them.Which …

Creating a Persistent Data Object In Django

I have a Python-based maximum entropy classifier. Its large, stored as a Pickle, and takes about a minute to unserialize. Its also not thread safe. However, it runs fast and can classify a sample (a si…

How to catch specific exceptions on sqlalchemy?

I want to catch specific exceptions like UniqueViolation on sqlalchemy.But sqlalchemy throw exceptions only through IntegrityError.So I catched specific exceptions with below code.except sqlalchemy.exc…

numpy.linalg.LinAlgError: SVD did not converge in Linear Least Squares on first run only

I have been wrestling with a known and documented SVD converge issue. Having read up on similar issues raised by others, I have double checked my data and reduced this to a tiny DataFrame - 10 rows/2 c…

Seaborn kde plot plotting probabilities instead of density (histplot without bars)

I have a question about seaborn kdeplot. In histplot one can set up which stats they want to have (counts, frequency, density, probability) and if used with the kde argument, it also applies to the kde…

How can I improve my code for euler 14?

I solved Euler problem 14 but the program I used is very slow. I had a look at what the others did and they all came up with elegant solutions. I tried to understand their code without much success.Her…

Visual Studio Code not recognizing Python import and functions

What do the squiggly lines represent in the image? The actual error the flags up when I hover my mouse over the squiggly line is: Import "pyspark.sql.functions" could not be resolvedPylance …

Pandas interpolate() backwards in dataframe

Going forward, interpolate works great:name days 0 a NaN 1 a NaN 2 a 2 3 a 3 4 a NaN 5 a NaN records.loc[:, days].interpolate(…

PEP8 for long methods name [duplicate]

This question already has an answer here:How to choose proper variable names for long names in python(1 answer)Closed 5 years ago.What is the PEP8 correct way for long methods name? I have a unit test…

POST method to upload file with json object in python flask app

I am stuck in a problem where I am trying to build single API which will upload file along with json object. I need this API to create webhook.Using multi part, I am able to upload file and in option f…