In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use Numpy library to deal with datetime type in Python". In daily operation, I believe many people have doubts about how to use Numpy library to deal with datetime type in Python. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to use Numpy library to deal with datetime type in Python". Next, please follow the editor to study!
Preface
With regard to time processing, the processing time modules included in Python are time, datetime, calendar, as well as extended third-party libraries, such as dateutil, and so on. In these ways, you can use Python to deal with time at will. When we use the NumPy library for data analysis, how to convert time?
Since the NumPy 1.7 release, its core array (ndarray) object supports datetime-related functions. Because the data type name 'datetime'' is already used in the datetime module that comes with Python, the type of time data in NumPy is called 'datetime64'.
When a single time format string is converted to a datetime object of numpy, an object can be instantiated using datetime64, as shown below:
# time string to numpy.datetime64datetime_nd=np.datetime64 ('2019-01-01') print (type (datetime_nd)) #
Conversely, the datetime object of numpy is converted to a time format string, using the datetime_as_string () function, as follows:
# numpy.datetime64 to time string datetime_str=np.datetime_as_string (datetime_nd) print (type (datetime_str)) #
When you create an array of datetime objects (array) for numpy from an array of time-formatted strings, you can directly use the numpy.array () function to specify that the elements in the array, such as' datetime64', are of type 'datetime64', as follows:
Datetime_array = np.array ([2019-01-01-02], dtype='datetime64') print (datetime_array) # ['2019-01-05', 2019-01-02 '2019-01-03'] print (type (datetime_array)) # print (datetime_array [0]) #
You can also use the numpy.arange () function to create an array of datetime objects (array) of numpy in a given time starting range. When dtype is specified as' datetime64', the default time interval is day, as shown below:
Datetime_array = np.arange ('2019-01-05) print (datetime_array) # [' 2019-01-05' 2019-01-06' 2019-01-07' 2019-01-08' 2019-01-09']
By setting the dtype parameter in the numpy.arange () function, you can adjust the time interval, such as generating a time array at intervals of year, month, week, or even hours, minutes, and milliseconds, which is the same as Python's datetime module, which is divided into date units and time units. As follows:
# generate year datetime arraydatetime_array = np.arange ('2018-01-01', dtype='datetime64 [Y]') print (datetime_array) # ['2018' '20119] # generate month datetime arraydatetime_array = np.arange (' 2019-01-01') Dtype='datetime64 [M]') print (datetime_array) # ['2019-01', 2019-02', 2019-03, 2019-04, 2019-05, 2019-06, 2019-07, 2019-08, 2019-09] # generate week datetime arraydatetime_array = np.arange (2019-01-05) Dtype='datetime64 [W]') print (datetime_array) # ['2019-01-03' '2019-01-10' '2019-01-17' '2019-01-24' '2019-01-31'] # generate ms datetime arraydatetime_array = np.arange Dtype='datetime64 [ms]') print (datetime_array) # ['2019-01-05T00VOV 00.000'' 2019-01-05T00:00:00.001'# '2019-01-05T00RV 00.002'...' 2019-01-09T23:59:59.997'# '2019-01-09T23VOLNFOR 59.998'' 2019-01-09T23VOT23MORAPHY 59.999']
To convert numpy.datetime64 to datetime format to datetime format, you can convert data types using the astype () method, as follows:
# convert numpy.datetime64 to datetime format datetime_df=datetime_nd.astype (datetime.datetime) print (type (datetime_df)) #
In addition, numpy also provides the function of datetime.timedelta class, which supports the operation of two time objects to get a numerical value in the form of time units. Because the core array (ndarray) object of numpy does not have a physical quantity system (physical quantities system), the timedelta64 data type is created to complement datetime64. The combination of datetime and timedelta provides a simpler datetime calculation method. As follows:
# numpy.datetime64 calculationsdatetime_delta = np.datetime64 ('2009-01-01')-np.datetime64 ('2008-01-01') print (datetime_delta) # 366daysprint (type (datetime_delta)) # datetime_delta = np.datetime64 ('2009') + np.timedelta64 (20,'D') print (datetime_delta) # 2009-01-21datetime_delta = np.datetime64 ('2011-06-15T0000') + np.timedelta64 (12 'h') print (datetime_delta) # 2011-06-15T12:00datetime_delta = np.timedelta64 (1recordW') / np.timedelta64 (1recoveryD') print (datetime_delta) # 7.0so far The study on "how to use the Numpy library to deal with datetime types in Python" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.