当前位置:网站首页>Datetime data type - min() get the earliest date and date_ Range() creates a date range, timestamp() creates a timestamp, and tz() changes the time zone

Datetime data type - min() get the earliest date and date_ Range() creates a date range, timestamp() creates a timestamp, and tz() changes the time zone

2022-06-26 04:50:00 I am a little monster

Catalog

min(): Get the earliest date

date_range: Create date range

Offset parameter

Timestamp: Create timestamps

tz: The time zone

tz Parameter to specify the time zone

tz_localize: Used to encode the time zone

to_convert: Change time zone


min(): Get the earliest date

>>> import pandas as pd
>>> ebola=pd.read_csv(r'D:\pandas Flexible use \pandas_for_everyone-master\data/country_timeseries.csv',parse_dates=[0])
>>> print(ebola['Date'].min())
2014-03-22 00:00:

date_range: Create date range

>>> r=pd.date_range(start='1999-12-01',end='1999-12-31')
>>> print(r)
DatetimeIndex(['1999-12-01', '1999-12-02', '1999-12-03', '1999-12-04',
               '1999-12-05', '1999-12-06', '1999-12-07', '1999-12-08',
               '1999-12-09', '1999-12-10', '1999-12-11', '1999-12-12',
               '1999-12-13', '1999-12-14', '1999-12-15', '1999-12-16',
               '1999-12-17', '1999-12-18', '1999-12-19', '1999-12-20',
               '1999-12-21', '1999-12-22', '1999-12-23', '1999-12-24',
               '1999-12-25', '1999-12-26', '1999-12-27', '1999-12-28',
               '1999-12-29', '1999-12-30', '1999-12-31'],
              dtype='datetime64[ns]', freq='D')

Offset parameter

date_range There is a parameter in the freq, Used to select from the range you specify , You can choose working days (B)、 At the beginning of (MS)、 Month end working day (BM)、 The working day at the beginning of the quarter (BQS) etc. .

Some examples are given below ,freq All possible reference values can be referred to date_range Create date range freq Parameter value table _ I am a little monster blog -CSDN Blog

>>> r=pd.date_range(start='1999-12-01',end='1999-12-31',freq='B')
>>> print(r)
DatetimeIndex(['1999-12-01', '1999-12-02', '1999-12-03', '1999-12-06',
               '1999-12-07', '1999-12-08', '1999-12-09', '1999-12-10',
               '1999-12-13', '1999-12-14', '1999-12-15', '1999-12-16',
               '1999-12-17', '1999-12-20', '1999-12-21', '1999-12-22',
               '1999-12-23', '1999-12-24', '1999-12-27', '1999-12-28',
               '1999-12-29', '1999-12-30', '1999-12-31'],
              dtype='datetime64[ns]', freq='B')

>>> r=pd.date_range(start='1999-12-01',end='1999-12-31',freq='BM')
>>> print(r)
DatetimeIndex(['1999-12-31'], dtype='datetime64[ns]', freq='BM'

>>> r=pd.date_range(start='1999-1-01',end='1999-12-31',freq='BQS')
>>> print(r)
DatetimeIndex(['1999-01-01', '1999-04-01', '1999-07-01', '1999-10-01'], dtype='datetime64[ns]', freq='BQS-JAN')

Timestamp: Create timestamps

>>> depart=pd.Timestamp('2022-08-29 07:00',tz='US/Eastern')#tz Used to specify the time zone , It will be briefly introduced under the timestamp 
>>> print(depart)
2022-08-29 07:00:00-04:00
>>> print(type(depart))
<class 'pandas._libs.tslibs.timestamps.Timestamp'>

tz: The time zone

python Of pytz The library is designed to handle time zones

tz Parameter to specify the time zone

>>> depart=pd.Timestamp('2022-08-29 07:00',tz='US/Eastern')
>>> print(depart)
2022-08-29 07:00:00-04:00

tz_localize: Used to encode the time zone

>>> arrive=pd.Timestamp('2022-08-29 07:00')
>>> print(arrive)
2022-08-29 07:00:00
>>> arrive=arrive.tz_localize('US/Eastern')# Specify the time zone of the created arrival time as the eastern United States 
>>> print(arrive)
2022-08-29 07:00:00-04:00

to_convert: Change time zone

The transformation time zone here must be assigned to the original variable of the date , To change the time zone of the date variable

>>> depart=pd.Timestamp('2022-08-29 07:00',tz='US/Eastern')
>>> arrive=pd.Timestamp('2022-08-29 12:00')
>>> arrive=arrive.tz_localize('US/Pacific')
>>> print(arrive.tz_convert('US/Eastern'))
2022-08-29 15:00:00-04:00
# Only time in the same time zone can be calculated , The above time zone conversion is not assigned to arrive, So there is no real arrive Your time zone has changed , Here, you need to convert before you can perform the operation 
>>> print(arrive.tz_convert('US/Eastern')-depart)
0 days 08:00:00
原网站

版权声明
本文为[I am a little monster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180510153877.html