<p>如果您有一个具有对象数据类型的pandas序列,那么可以首先使用<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html" rel="nofollow noreferrer">pd.to_datetime()</a>将其转换为日期时间序列</p>
<pre><code>df[col] = pd.to_datetime(your_series, format = '%Y-%m-%d %H:%M:%S', errors ='coerce')
</code></pre>
<p>使用<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.dt.tz.html" rel="nofollow noreferrer">series.dt.tz</a>检查是否可以识别时区</p>
<pre><code>df[col].dt.tz
</code></pre>
<p>如果它不知道时区,我们应该使用<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.tz_localize.html" rel="nofollow noreferrer">series.dt.tz_localize()</a>使它知道时区。另外,请阅读此函数的不明确且不存在的参数</p>
<pre><code>df[col] = your_series[col].dt.tz_localize('UTC')
</code></pre>
<p>现在通过<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.dt.tz_convert.html" rel="nofollow noreferrer">series.dt.tz_convert()</a>将这个序列转换成所需的时区</p>
<pre><code>df[col] = your_series[col].dt.tz_convert('US/Eastern')
</code></pre>
<p>以上方法将考虑夏令时。如果您想查看更多时区,可以pip安装pytz和</p>
<pre><code>import pytz
pytz.common_timezones
</code></pre>