Python酷库之旅-第三方库Pandas(150)

CSDN 2024-10-23 16:35:01 阅读 62

目录

一、用法精讲

681、pandas.Timestamp.now方法

681-1、语法

681-2、参数

681-3、功能

681-4、返回值

681-5、说明

681-6、用法

681-6-1、数据准备

681-6-2、代码示例

681-6-3、结果输出

682、pandas.Timestamp.replace方法

682-1、语法

682-2、参数

682-3、功能

682-4、返回值

682-5、说明

682-6、用法

682-6-1、数据准备

682-6-2、代码示例

682-6-3、结果输出

683、pandas.Timestamp.round方法

683-1、语法

683-2、参数

683-3、功能

683-4、返回值

683-5、说明

683-6、用法

683-6-1、数据准备

683-6-2、代码示例

683-6-3、结果输出

684、pandas.Timestamp.strftime方法

684-1、语法

684-2、参数

684-3、功能

684-4、返回值

684-5、说明

684-6、用法

684-6-1、数据准备

684-6-2、代码示例

684-6-3、结果输出

685、pandas.Timestamp.strptime方法

685-1、语法

685-2、参数

685-3、功能

685-4、返回值

685-5、说明

685-6、用法

685-6-1、数据准备

685-6-2、代码示例

685-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

681、pandas.Timestamp.now方法
681-1、语法

<code># 681、pandas.Timestamp.now方法

classmethod pandas.Timestamp.now(tz=None)

Return new Timestamp object representing current time local to tz.

Parameters:

tz

str or timezone object, default None

Timezone to localize to.

681-2、参数

681-2-1、tz(可选,默认值为None):str或pytz.timezone或dateutil.tz.tzinfo指定时区。如果未提供,返回的是本地时间,可以使用字符串(如'America/New_York')或者其他时区对象。

681-3、功能

        获取当前的时间戳,类似于Python的datetime.now(),但是返回的是pandas.Timestamp类型,可以方便地进行时间序列分析和处理。

681-4、返回值

        返回一个pandas.Timestamp对象,表示当前时间,如果指定了时区,将返回对应时区的当前时间;否则,返回本地时间。

681-5、说明

        无

681-6、用法
681-6-1、数据准备

681-6-2、代码示例

# 681、pandas.Timestamp.now方法

import pandas as pd

# 获取当前本地时间的时间戳

current_time_local = pd.Timestamp.now()

print(current_time_local)

# 获取当前UTC时间的时间戳

current_time_utc = pd.Timestamp.now(tz='UTC')code>

print(current_time_utc)

# 获取当前特定时区的时间戳

current_time_ny = pd.Timestamp.now(tz='America/New_York')code>

print(current_time_ny)

681-6-3、结果输出

# 681、pandas.Timestamp.now方法

# 2024-10-14 21:44:55.799025

# 2024-10-14 13:44:55.812327+00:00

# 2024-10-14 09:44:55.903473-04:00

682、pandas.Timestamp.replace方法
682-1、语法

# 682、pandas.Timestamp.replace方法

pandas.Timestamp.replace(year=None, month=None, day=None, hour=None, minute=None, second=None, microsecond=None, nanosecond=None, tzinfo=<class 'object'>, fold=None)

Implements datetime.replace, handles nanoseconds.

Parameters:

year

int, optional

month

int, optional

day

int, optional

hour

int, optional

minute

int, optional

second

int, optional

microsecond

int, optional

nanosecond

int, optional

tzinfo

tz-convertible, optional

fold

int, optional

Returns:

Timestamp with fields replaced

682-2、参数

682-2-1、year(可选,默认值为None)整数,表示替换的年份,如果不指定,年份保持不变。

682-2-2、month(可选,默认值为None)整数,表示替换的月份,如果不指定,月份保持不变。有效范围为1到12。

682-2-3、day(可选,默认值为None)整数,表示替换的日期,如果不指定,日期保持不变,有效范围依赖于年份和月份。

682-2-4、hour(可选,默认值为None)整数,表示替换的小时,如果不指定,小时保持不变,有效范围为0到23。

682-2-5、minute(可选,默认值为None)整数,表示替换的分钟,如果不指定,分钟保持不变,有效范围为0到59。

682-2-6、second(可选,默认值为None)整数,表示替换的秒,如果不指定,秒保持不变,有效范围为0到59。

682-2-7、microsecond(可选,默认值为None)整数,表示替换的微秒,如果不指定,微秒保持不变,有效范围为0到999999。

682-2-8、nanosecond(可选,默认值为None)整数,表示替换的纳秒,如果不指定,纳秒保持不变,有效范围为0到999999999。

682-2-9、tzinfo(可选,默认值为<class 'object'>)timezone, 表示替换时区信息,如果不指定,时区保持不变。

682-2-10、fold(可选,默认值为None)整数,用于解决夏令时冲突的参数,如果不指定,保持不变,0表示前一个时间,1表示后一个时间。

682-3、功能

        用于生成一个新的时间戳,允许用户在需要更改某些时间部分的同时保持其他部分不变,通过指定某些参数,你可以灵活地修改时间戳。

682-4、返回值

        返回一个新的Timestamp对象,这个对象的指定项被替换为传入的值,而其他未指定的项则保持不变。

682-5、说明

        无

682-6、用法
682-6-1、数据准备

682-6-2、代码示例

# 682、pandas.Timestamp.replace方法

import pandas as pd

# 创建一个时间戳

ts = pd.Timestamp('2024-10-14 21:58:45')

# 替换年和月

new_ts = ts.replace(year=2023, month=12)

print(new_ts)

682-6-3、结果输出

# 682、pandas.Timestamp.replace方法

# 2023-12-14 21:58:45

683、pandas.Timestamp.round方法
683-1、语法

# 683、pandas.Timestamp.round方法

pandas.Timestamp.round(freq, ambiguous='raise', nonexistent='raise')code>

Round the Timestamp to the specified resolution.

Parameters:

freq

str

Frequency string indicating the rounding resolution.

ambiguous

bool or {‘raise’, ‘NaT’}, default ‘raise’

The behavior is as follows:

bool contains flags to determine if time is dst or not (note that this flag is only applicable for ambiguous fall dst dates).

‘NaT’ will return NaT for an ambiguous time.

‘raise’ will raise an AmbiguousTimeError for an ambiguous time.

nonexistent

{‘raise’, ‘shift_forward’, ‘shift_backward, ‘NaT’, timedelta}, default ‘raise’

A nonexistent time does not exist in a particular timezone where clocks moved forward due to DST.

‘shift_forward’ will shift the nonexistent time forward to the closest existing time.

‘shift_backward’ will shift the nonexistent time backward to the closest existing time.

‘NaT’ will return NaT where there are nonexistent times.

timedelta objects will shift nonexistent times by the timedelta.

‘raise’ will raise an NonExistentTimeError if there are nonexistent times.

Returns:

a new Timestamp rounded to the given resolution of

freq

Raises:

ValueError if the freq cannot be converted

Notes

If the Timestamp has a timezone, rounding will take place relative to the local (“wall”) time and re-localized to the same timezone. When rounding near daylight savings time, use nonexistent and ambiguous to control the re-localization behavior.

683-2、参数

683-2-1、freq(必须)str或DateOffset,指定要四舍五入到的频率,常见的频率字符串包括:'s'(秒)、'min'(分钟)、'h'(小时)、'D'(天)、'B'(工作日)、'M'(月)、'Q'(季度)、'Y'(年)等。

683-2-2、ambiguous(可选,默认值为'raise')bool或{'raise', 'infer', 'NaT'},如果时间戳在夏令时(DST)切换期间采用,可以选择如何处理模糊的时间;如果设置为'raise',将抛出异常;'infer'会尝试根据时间戳的上下文推断;'NaT'会返回缺失值。

683-2-3、nonexistent(可选,默认值为'raise'){'raise', 'shift_forward', 'NaT'},如果四舍五入后得到的时间戳在给定频率下是不存在的(例如,在切换夏令时时),该参数决定了如何处理,'raise'会抛出异常;'shift_forward'会将时间戳移动到下一个有效时间;'NaT'会返回缺失值。

683-3、功能

        根据指定的频率对时间戳进行四舍五入,这允许用户快速调整时间戳,使其符合特定的时间间隔需求,从而在数据分析中提高一致性和可读性。

683-4、返回值

        返回一个新的Timestamp对象,该对象是根据给定频率四舍五入后的结果,如果原始时间戳已经符合指定频率,则返回原始时间戳。

683-5、说明

        无

683-6、用法
683-6-1、数据准备

683-6-2、代码示例

# 683、pandas.Timestamp.round方法

import pandas as pd

# 创建一个时间戳

ts = pd.Timestamp('2024-10-14 22:10:45')

# 四舍五入到最近的分钟

rounded_ts = ts.round(freq='min')code>

print(rounded_ts)

683-6-3、结果输出

# 683、pandas.Timestamp.round方法

# 2024-10-14 22:11:00

684、pandas.Timestamp.strftime方法
684-1、语法

# 684、pandas.Timestamp.strftime方法

pandas.Timestamp.strftime(format)

Return a formatted string of the Timestamp.

Parameters:

format

str

Format string to convert Timestamp to string. See strftime documentation for more information on the format string: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior.

684-2、参数

684-2-1、format(必须)字符串,一个字符串,定义了输出的日期时间格式,格式字符串可以包含以下占位符:

%Y:四位年份%y:两位年份%m:月份(01-12)%b:月份的缩写(如Jan, Feb等)%B:完整的月份名称(如January, February等)%d:日(01-31)%H:小时(00-23)%I:小时(01-12)%M:分钟(00-59)%S:秒(00-59)%p:AM或PM%A:星期几的全称(如Monday, Tuesday等)%a:星期几的缩写(如Mon, Tue等)

684-3、功能

        用于将时间戳格式化为字符串,以指定的格式输出,这在需要将时间戳转换为可读字符串或特定格式时非常有用,例如在生成报告或显示日期时间时。

684-4、返回值

        返回指定格式的日期时间字符串。

684-5、说明

        无

684-6、用法
684-6-1、数据准备

684-6-2、代码示例

# 684、pandas.Timestamp.strftime方法

import pandas as pd

# 创建一个时间戳

ts = pd.Timestamp('2024-10-14 22:25:30')

# 按照指定格式输出字符串

formatted_str = ts.strftime('%Y-%m-%d %H:%M:%S')

print(formatted_str)

684-6-3、结果输出

# 684、pandas.Timestamp.strftime方法

# 2024-10-14 22:25:30

685、pandas.Timestamp.strptime方法
685-1、语法

# 685、pandas.Timestamp.strptime方法

classmethod pandas.Timestamp.strptime(string, format)

Function is not implemented. Use pd.to_datetime().

685-2、参数

685-2-1、string(必须)字符串,表示要解析的日期时间字符串。

685-2-2、format(必须)字符串,指定字符串的解析格式,类似于strftime中使用的格式符号。

685-3、功能

        用于将字符串解析为时间戳对象,它根据指定的格式将输入的日期时间字符串转换为一个Timestamp实例,这在处理和解析来自数据源的日期时间字符串时非常有用。

685-4、返回值

        返回一个Timestamp对象。

685-5、说明

        无

685-6、用法
685-6-1、数据准备

685-6-2、代码示例

# 685、pandas.Timestamp.strptime方法

import pandas as pd

from datetime import datetime

# 定义一个日期时间字符串

date_str = '2024-10-14 22:30:30'

# 使用datetime.strptime将字符串解析为datetime对象

dt = datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')

# 转换为pandas Timestamp

ts = pd.Timestamp(dt)

print(ts)

685-6-3、结果输出

# 685、pandas.Timestamp.strptime方法

# 2024-10-14 22:30:30

二、推荐阅读

1、Python筑基之旅
2、Python函数之旅
3、Python算法之旅
4、Python魔法之旅
5、博客个人主页


声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。