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

What are the seven timestamp handling functions commonly used in Pandas

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "what are the seven timestamp handling functions commonly used in Pandas". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "what are the seven timestamp handling functions commonly used in Pandas" can help you solve the problem.

In industries such as retail, economy and finance, data are always changing due to money and sales, and all the data generated are highly time-dependent. If the data is not time-stamped or marked, it is actually difficult to manage all the collected data. The Python program allows us to use NumPy timedelta64 and datetime64 to manipulate and retrieve time series data. Time series functionality is also available in the sklern library, but Pandas provides us with more useful functions.

There are four time-related concepts in the Pandas library

Date time: date time represents a specific date and time and their respective time zones. Its data type in pandas is datetime64 [ns] or datetime64 [ns, tz].

Time increments: time increments represent time differences, and they can be different units. Example: "day, hour, minus sign" and so on. In other words, they are subclasses of date and time.

Time span: the time span is called the correlation frequency within a fixed period. The data type for the time span is period [freq].

Date offset: the date offset helps to calculate the selected date from the current date, and the date offset has no specific data type in pandas.

Time series analysis is critical because they can help us understand the factors that affect trends or system patterns over time. With the help of data visualization, analyze and make follow-up decisions.

Now let's look at a few examples of using these functions

1. Find the name of a certain day on a specific date import pandas as pdday = pd.Timestamp ('2021 Universe 5') day.day_name ()

The above program displays the name of a specific date. The first step is to import panda and use the Timestamp and day_name functions. The "Timestamp" function is used to enter the date, and the "day_name" function is used to display the name of the specified date.

2. Perform the arithmetic calculation import pandas as pdday = pd.Timestamp ('2021 day) day1 = day + pd.Timedelta ("3 day") day1.day_name () day2 = day1 + pd.offsets.BDay () day2.day_name ()

In the first code, the name of the date after three days is displayed. The "Timedelta" function allows you to enter the time difference of any day unit (day, hour, minute, second).

In the second code, the "offsets.BDay ()" function is used to display the next working day. In other words, this means that after Friday, the next working day is Monday.

3. Use time zone information to operate the conversion date and time

Get information about the time zone

Import pandas as pdimport numpy as npfrom datetime import datetimedat_ran = dat_ran.tz_localize ("UTC") dat_ran

Convert to US time zone

Dat_ran.tz_convert ("US/Pacific")

The goal of the code is to change the time zone of the date. First, you need to find the current time zone. This is done by the "tz_localize ()" function. We now know that the current time zone is "UTC". Use the "tz_convert ()" function to convert to the US / Pacific time zone.

4. Use the date and time stamp import pandas as pdimport numpy as npfrom datetime import datetimedat_ran = pd.date_range (start = '1gamma 2021, end =' 1gamma 2021, freq = 'Min') print (type)

5. Creation date series import pandas as pdimport numpy as npfrom datetime import datetimedat_ran = pd.date_range (start = '1max 1max 2021A, end =' 1max 5max 2021A, freq = 'Min') print (dat_ran)

The above code generates a range of dates. Using the "date_range" function, enter the start and end dates to get the dates in that range.

6. Operation date sequence import pandas as pdfrom datetimeimport datetimeimport numpy as npdat_ran = pd.date_range (start = '1date' 1date' 2019, end =' 1ap08Accord 2019) df = pd.DataFrame (dat_ran, columns = ['date']) df [' data'] = np.random.randint (0,100, size = (len (dat_ran)) print (df.head (5))

In the above code, you use the "DataFrame" function to convert the string type to dataframe. Finally, the "np.random.randint ()" function randomly generates some hypothetical data.

7. Use the timestamp data to slice the data import pandas as pdfrom datetimeimport datetimeimport numpy as npdat_ran = pd.date_range (start = '1max 2019, end =' 1max 2019, freq = 'Min') df = pd.DataFrame (dat_ran, columns = [' date']) df ['data'] = np.random.randint (0,100) Size = (len (dat_ran) string_data = [str (x) for x in dat_ran] print (string_data [1:5])

The above code is a continuation of Article 6. After creating a dataframe and mapping it to a random number, slice the list.

This is the end of the introduction to "what are the seven timestamp handling functions commonly used in Pandas". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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