Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of Pandas time Series data of Python

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "Pandas Time Series Data Example Analysis of Python". The explanation in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian and go deep into it slowly to study and learn "Pandas Time Series Data Example Analysis of Python" together.

Pandas Time Series Data

Time series data is a very important kind of data in data analysis. Things evolve over time, and data is generated at various points in time.

Time in Python-datetime module

Python's standard library datetime supports creation and manipulation of time, and Pandas 'time-series module builds on datetime.

1. import datetime = datetime.datetime.now ()#2022 -03-06 18:43:49.506048

The full picture of now is as follows, which is a datetime object:

now: datetime.datetime(2022, 3, 6, 18, 37, 10, 132078)

datetime.datetime object Common methods:

(1)strftime(): Transforms the format of datetime objects. Write the target format using placeholders containing %, such as:

newForm = now.strftime("%Y-%m-%d (%H:%M:%S)")# 2022-03-06 (18:52:31) is a string type

Each placeholder has the following meaning

% y Two-digit year representation (00 - 99) % Y Four-digit year representation (000 - 9999) % m Month (01 - 12) % d Day of Month (0 - 31) % H Hours of 24 Hours (0 - 23) % I Hours of 12 Hours (01 - 12) % M Minutes (00 = 59) % S Seconds (00 - 59) % a Local Simplified Week Name % A Local Full Week Name % b Local Simplified Month Name % B Local Full Month Name % c Local Corresponding Date and Time Representation % j Day of Year (001 - 366) % p Equivalent of local A.M. or P.M.% U Number of weeks of the year (00 - 53) Sunday is the beginning of the week % w Week (0 - 6), Sunday is the beginning of the week % W Number of weeks in the year (00 - 53) Monday is the beginning of the week % x Local corresponding date representation % X Local corresponding time representation % Z Name of current time zone % % % sign itself 2. Specify time

Pass the corresponding parameters into datetime.datetime(), and specify them in descending order by default.

birthday = datetime.datetime(2001, 1, 11) #print(birthday) specified by default

It can also be specified by parameters, where year, month and day must be specified.

yesterday = pd.Timestamp(2022, 3, 5)#2022 -03-05 00:00:00now = pd.Timestamp("now")#2022 -03-06 19:13:56.5076043. Operation

(1)datetime.datetime objects support subtraction, resulting in a datetime.timedelta object

delta = yearday- birthday # 7723 days, 0:00:00 II. Pandas processing timing sequence 1.pd.Timestamp()

pd.Timestamp() is the main function of Pandas to define event time, and supports richer construction methods to define time.

(1) Based on datetime.datetime object

now = pd.Timestamp(datetime.datetime.now())print(now) # 2022-03-06 19:07:07.253402

(2)based on string

today = pd.Timestamp("2022-03-06")print(today) # 2022-03-06 00:00:00

(3)More specified parameters

yesterday = pd.Timestamp(2022, 3, 5) # 2022-03-05 00:00:00now = pd.Timestamp("now") # 2022-03-06 19:13:56.507604

(4)from a timestamp, a

pd.Timestamp(1646565103.114923, unit="s") # unit Specifies the unit as seconds 2.pd.Timedelta()

pd.Timedelta() is used to create a time difference object and has a similar construction method as pd.Timestamp().

(1)Create from string

oneDay = pd.Timedelta("1 days") # 1 days 00:00:00duration = pd.Timedelta("2 days 2 hours") # 1 days 02:00:00

(2)Specify parameter creation

oneDay = pd.Timedelta(days=1) # 1 days 00:00:003. The operation is similar to the operation of the datetime module. Subtracting the Timestamp objects of the pandas results in the Timedelta object. 4. time index

Data tables often use time as an index, pandas supports the creation of long time series

(1) pd.to_datetime()

pd.to_datetime() supports converting time objects and time-like strings into datetimeIndex objects.

index = pd.to_datetime(["03/06/2022", datetime.datetime.now()])print(index)# DatetimeIndex(['2022-03-06 00:00:00', '2022-03-06 19:29:44.855267'], dtype='datetime64[ns]', freq=None)

(2) pd.date_range()

pd.date_range() can be given a start time or an end time, and specifies the cycle data, cycle frequency, will automatically generate time index data in this range:

index = pd.date_range(start="2022-01-01", periods=10)print(index)# DatetimeIndex(['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04', '2022-01-05', '2022-01-06', '2022-01-07', '2022-01-08', '2022-01-09', '2022-01-10'], dtype='datetime64[ns]', freq='D')

If you want to skip the rest day, you can use the pd.bdate_range() function

Thank you for reading, the above is the content of "Pandas time series data example analysis of Python", after learning this article, I believe that everyone has a deeper understanding of the problem of Pandas time series data example analysis of Python, and the specific use needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report