Pandas DataFrame.to_timestamp(~) 方法将行索引或列索引转换为  DatetimeIndex  。
参数
1.freq | string | optional
默认为  PeriodIndex  中使用的频率。
2. how | string | optional
是否使用 PeriodIndex 的开始或结束时间:
| 值 | 说明 | 
|---|---|
| 
 | 使用该期间的开始时间。 | 
| 
 | 使用该期间的结束时间。 | 
默认情况下,how="start" 。
3. axis | string | optional
是否转换索引或列标签:
| 轴 | 说明 | 
|---|---|
| 
 | 将索引转换为   | 
| 
 | 将列标签转换为   | 
默认情况下,axis=0 。
4. copy | boolean | optional
- 
如果 True,则返回新的DataFrame。修改此 DataFrame 不会改变源 DataFrame,反之亦然。
- 
如果 False,则不会创建新的 DataFrame - 修改返回的 DataFrame 将改变源 DataFrame,反之亦然。
默认情况下,copy=True 。
返回值
DataFrame 的索引或列标签转换为  DatetimeIndex  。
例子
基本用法
考虑以下 DataFrame :
index_period = pd.PeriodIndex(["2020-12-25"], freq="D")
df = pd.DataFrame({"A":[2]}, index=index_period)
df
            A
2020-12-25  2这里, df 的索引是  PeriodIndex  类型:
df.index
PeriodIndex(['2020-12-25'], dtype='period[D]', freq='D')要将索引从  PeriodIndex  转换为  DatetimeIndex  :
df_converted = df.to_timestamp()
df_converted
            A
2020-12-25  2现在,检查转换后的索引的类型:
df_converted.index
DatetimeIndex(['2020-12-25'], dtype='datetime64[ns]', freq=None)我们看到 df_converted 的索引现在是  DatetimeIndex  。
指定频率
考虑以下带有  PeriodIndex  的 DataFrame :
index_period = pd.PeriodIndex(["2020-12-25","2020-12-26","2020-12-27"], freq="D")
df = pd.DataFrame({"A":[2,3,4]}, index=index_period)
df
             A
2020-12-25   2
2020-12-26   3
2020-12-27   4假设我们想删除有关日单位的信息。我们可以使用 freq 参数来执行此操作,如下所示:
df_converted = df.to_timestamp(freq="M")
df_converted
            A
2020-12-31  2
2020-12-31  3
2020-12-31  4现在,所有日单位都设置为月底 (31)。
作为参考,这是 df_converted 的索引:
df_converted.index
DatetimeIndex(['2020-12-31', '2020-12-31', '2020-12-31'], dtype='datetime64[ns]', freq=None)指定如何
考虑以下 DataFrame :
index_period = pd.PeriodIndex(["2020-12-25"], freq="D")
df = pd.DataFrame({"A":[2]}, index=index_period)
df
            A
2020-12-25  2默认情况下, how="s" ,这意味着使用周期的开始:
df.to_timestamp(how="s")
            A
2020-12-25  2要使用句点结尾,请设置 how="e" :
df.to_timestamp(how="e")
                               A
2020-12-25 23:59:59.999999999  2相关用法
- Python Pandas DataFrame to_csv方法用法及代码示例
- Python Pandas DataFrame to_period方法用法及代码示例
- Python Pandas DataFrame to_json方法用法及代码示例
- Python Pandas DataFrame to_numpy方法用法及代码示例
- Python Pandas DataFrame to_dict方法用法及代码示例
- Python PySpark DataFrame toDF方法用法及代码示例
- Python PySpark DataFrame toJSON方法用法及代码示例
- Python PySpark DataFrame toPandas方法用法及代码示例
- Python Pandas DataFrame tz_convert方法用法及代码示例
- Python Pandas DataFrame tail方法用法及代码示例
- Python Pandas DataFrame transform方法用法及代码示例
- Python Pandas DataFrame truncate方法用法及代码示例
- Python Pandas DataFrame tz_localize方法用法及代码示例
- Python Pandas DataFrame truediv方法用法及代码示例
- Python Pandas DataFrame transpose方法用法及代码示例
- Python Pandas DataFrame tshift方法用法及代码示例
- Python Pandas DataFrame take方法用法及代码示例
- Python PySpark DataFrame tail方法用法及代码示例
- Python PySpark DataFrame transform方法用法及代码示例
- Python PySpark DataFrame take方法用法及代码示例
- Python Pandas DataFrame empty属性用法及代码示例
- Python Pandas DataFrame pop方法用法及代码示例
- Python Pandas DataFrame nsmallest方法用法及代码示例
- Python Pandas DataFrame sample方法用法及代码示例
- Python Pandas DataFrame items方法用法及代码示例
注:本文由纯净天空筛选整理自Isshin Inada大神的英文原创作品 Pandas DataFrame | to_timestamp method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
