Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23218

convert dataframe from florida EDT to the California timezone

$
0
0

A database in Florida stores data from California in Florida day light saving time. Now, I want to convert it to the California time.

Code:

import pandas as pdfrom pytz import timezonefrom datetime import datetime# Sample DataFrame (ensure it is defined correctly in your script)data = {'Datetime': pd.date_range(start='2023-11-04', periods=10, freq='H'),'Data': range(10)}invdf = pd.DataFrame(data)invdf.set_index('Datetime', inplace=True)# Time zone informationeastern = timezone('US/Eastern')# Localize the datetime to Eastern time zone considering daylight saving timeinvdf['Datetime'] = invdf.indexinvdf['Datetime'] = invdf['Datetime'].dt.tz_localize('US/Eastern', ambiguous='infer')# Convert to Los Angeles time (Pacific time)invdf['Datetime'] = invdf['Datetime'].dt.tz_convert('America/Los_Angeles')# Reset the index to updated datetimeinvdf.set_index('Datetime', inplace=True, drop=True)

Response:

---------------------------------------------------------------------------AmbiguousTimeError                        Traceback (most recent call last)Cell In[208], line 2      1 invdf['Datetime'] = invdf.index----> 2 invdf['Datetime'] = invdf['Datetime'].dt.tz_localize('US/Eastern', ambiguous='infer')      3 # Convert to Los Angeles time (Pacific time)      4 invdf['Datetime'] = invdf['Datetime'].dt.tz_convert('America/Los_Angeles')File /opt/conda/lib/python3.10/site-packages/pandas/core/accessor.py:112, in PandasDelegate._add_delegate_accessors.<locals>._create_delegator_method.<locals>.f(self, *args, **kwargs)    111 def f(self, *args, **kwargs):--> 112     return self._delegate_method(name, *args, **kwargs)File /opt/conda/lib/python3.10/site-packages/pandas/core/indexes/accessors.py:132, in Properties._delegate_method(self, name, *args, **kwargs)    129 values = self._get_values()    131 method = getattr(values, name)--> 132 result = method(*args, **kwargs)    134 if not is_list_like(result):    135     return resultFile /opt/conda/lib/python3.10/site-packages/pandas/core/indexes/datetimes.py:293, in DatetimeIndex.tz_localize(self, tz, ambiguous, nonexistent)    286 @doc(DatetimeArray.tz_localize)    287 def tz_localize(    288     self,   (...)    291     nonexistent: TimeNonexistent = "raise",    292 ) -> Self:--> 293     arr = self._data.tz_localize(tz, ambiguous, nonexistent)    294     return type(self)._simple_new(arr, name=self.name)File /opt/conda/lib/python3.10/site-packages/pandas/core/arrays/_mixins.py:81, in ravel_compat.<locals>.method(self, *args, **kwargs)     78 @wraps(meth)     79 def method(self, *args, **kwargs):     80     if self.ndim == 1:---> 81         return meth(self, *args, **kwargs)     83     flags = self._ndarray.flags     84     flat = self.ravel("K")File /opt/conda/lib/python3.10/site-packages/pandas/core/arrays/datetimes.py:1088, in DatetimeArray.tz_localize(self, tz, ambiguous, nonexistent)   1085     tz = timezones.maybe_get_tz(tz)   1086     # Convert to UTC-> 1088     new_dates = tzconversion.tz_localize_to_utc(   1089         self.asi8,   1090         tz,   1091         ambiguous=ambiguous,   1092         nonexistent=nonexistent,   1093         creso=self._creso,   1094     )   1095 new_dates_dt64 = new_dates.view(f"M8[{self.unit}]")   1096 dtype = tz_to_dtype(tz, unit=self.unit)File tzconversion.pyx:328, in pandas._libs.tslibs.tzconversion.tz_localize_to_utc()File tzconversion.pyx:656, in pandas._libs.tslibs.tzconversion._get_dst_hours()AmbiguousTimeError: 2023-11-05 01:00:00

Viewing all articles
Browse latest Browse all 23218

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>