Hướng dẫn dùng numpy datetime64 python

Đây là cách tôi làm điều đó.

import numpy as np

def dt2cal[dt]:
    """
    Convert array of datetime64 to a calendar array of year, month, day, hour,
    minute, seconds, microsecond with these quantites indexed on the last axis.

    Parameters
    ----------
    dt : datetime64 array [...]
        numpy.ndarray of datetimes of arbitrary shape

    Returns
    -------
    cal : uint32 array [..., 7]
        calendar array with last axis representing year, month, day, hour,
        minute, second, microsecond
    """

    # allocate output 
    out = np.empty[dt.shape + [7,], dtype="u4"]
    # decompose calendar floors
    Y, M, D, h, m, s = [dt.astype[f"M8[{x}]"] for x in "YMDhms"]
    out[..., 0] = Y + 1970 # Gregorian Year
    out[..., 1] = [M - Y] + 1 # month
    out[..., 2] = [D - M] + 1 # dat
    out[..., 3] = [dt - D].astype["m8[h]"] # hour
    out[..., 4] = [dt - h].astype["m8[m]"] # minute
    out[..., 5] = [dt - m].astype["m8[s]"] # second
    out[..., 6] = [dt - s].astype["m8[us]"] # microsecond
    return out

Nó được vector hóa trên các kích thước đầu vào tùy ý, nhanh chóng, trực quan, hoạt động trên numpy v1.15.4, không sử dụng gấu trúc.

Tôi thực sự muốn numpy hỗ trợ chức năng này, nó luôn được yêu cầu trong quá trình phát triển ứng dụng. Tôi luôn cảm thấy cực kỳ lo lắng khi phải cuộn đồ đạc của mình như thế này, tôi luôn cảm thấy như mình đang thiếu một chiếc ốp lưng.

11 hữu ích 2 bình luận chia sẻ

Hướng dẫn dot trong python

Trong Python, hàm dot [] của NumPy được sử dụng để trả về tích số chấm của các mảng đã cho. Nó chấp nhận hai mảng làm đối số và tính tích số chấm ...

Hướng dẫn dùng numpy dimensions python

Nội dung chínhNội dung chínhPython Numpy là gì?NumPy ArrayNumpy cài đặt như thế nào?Multi-dimensional numPy array là gì?Python NumPy Array v/s ListPython NumPy OperationsTham ...

Hướng dẫn dùng np transpose python

Hàm numpy.transpose [] là một trong những hàm quan trọng nhất trong phép nhân ma trận. Hàm này hoán vị hoặc bảo lưu kích thước của mảng đã cho và trả về mảng ...

Python pretty print list of lists

To make things interesting, lets try with a bigger matrix:matrix = [ [Ah!, We do have some Camembert, sir], [Its a bit, runny, sir], [Well,, as a matter of fact its, ...

How do you create an empty string array in python?

In Python, the tendency is usually that one would use a non-fixed size list [that is to say items can be appended/removed to it dynamically]. If you followed this, there would be no need to allocate ...

Python get array from index

Suppose we have the following array:import numpy as np a = np.arange[1, 10] a = a.reshape[len[a], 1] array[[[1], [2], [3], [4], [5], [6], [7], [8], ...

How do you do accuracy in python?

In machine learning, accuracy is one of the most important performance evaluation metrics for a classification model. The mathematical formula for calculating the accuracy of a machine learning model ...

Hướng dẫn toarray python

csr_matrix.toarray[order=None, out=None][source]#Return a dense ndarray representation of this matrix.Nội dung chính2. Using numpy.array[]2. Using numpy.asarray[]Using numpy.asarray[] method to ...

How do i remove pip from python?

Back to top Edit this pageToggle table of contents sidebar Usage#Unix/macOSpython -m pip uninstall [options] ... python -m pip uninstall [options] -r ...

Hướng dẫn dùng np.add python

Nội dung chínhPython Numpy là gì?NumPy ArrayNumpy cài đặt như thế nào?Multi-dimensional numPy array là gì?Python NumPy Array v/s ListPython NumPy OperationsTham khảoNội dung ...

Hướng dẫn is power python

Python NumPy power[] được sử dụng để tính toán các phần tử mảng đầu tiên được nâng lên thành lũy thừa từ các phần tử mảng thứ hai, tính theo phần tử. ...

Hướng dẫn matlab vs python stackoverflow

One may switch from MATLAB to Python because Python deals much better with complexity i.e. easier to write, debug and maintain complex code. One of the good reasons for that is, Python is a general ...

Hướng dẫn dùng numpy sorting python

Để sắp xếp các phần tử của mảng NumPy theo trình tự có thứ tự, hãy sử dụng numpy.sort[]. Bằng cách sử dụng điều này, bạn có thể sắp xếp một mảng N ...

Hướng dẫn python integrate array

How do you get only 5 inputs in python?View DiscussionImprove ArticleSave ArticleReadDiscussView DiscussionImprove ArticleSave ArticleThe developer often wants a user to enter multiple values or ...

How do you transpose an array in python?

numpy.transpose[a, axes=None][source]#Reverse or permute the axes of an array; returns the modified array.For an array a with two axes, transpose[a] gives the matrix transpose.Refer to ...

Hướng dẫn dùng np.double python

Numpy [Numeric Python]: là một thư viện toán học phổ biến và mạnh mẽ của Python. Cho phép làm việc hiệu quả với ma trận và mảng, đặc biệt là dữ liệu ...

Hướng dẫn python time arithmetic

Bài viết được sự cho phép của tác giả Nguyễn Chí ThứcNội dung chínhLấy Time hiện tại trong Pythontime module trong Pythoncalendar Module trong PythonHàm ...

Hướng dẫn extrapolation python pandas

Can you reverse a number in python?Example 1: Reverse a Number using a while loopnum = 1234 reversed_num = 0 while num != 0: digit = num % 10 reversed_num = reversed_num * 10 + digit num //= 10 ...

What is pandas cheat sheet?

March 4, 2020If you’re interested in working with data in Python, you’re almost certainly going to be using the pandas library. But even when you’ve learned pandas — perhaps in our ...

Hướng dẫn dùng numpy std python

Để tìm độ lệch chuẩn của một mảng trong Python, hãy sử dụng hàm numpy.std []. Độ lệch chuẩn là căn bậc hai của giá trị trung bình của các độ lệch bình ...

Hướng dẫn inverse empirical cdf python

We can create the ECDF with import numpy as np from statsmodels.distributions.empirical_distribution import ECDF ecdf = ECDF[[3, 3, 1, 4]] and obtain then ECDF at point with ecdf[x] However, what if ...

How do you partition an array in python?

Python is famous for its English-like commands, code readability, and its simple programming syntax. It also provides its users with a wide array of data structures and list is one of them. The List ...

Hướng dẫn slicing python

Indexing cũng giống với các kiểu dữ liệu khác trong python, như tuple và list.import numpy as np F = np.array[[1, 1, 2, 3, 5, 8, 13, 21]] print the first element of F, i.e. the ...

Hướng dẫn dùng define reshaping python

Hàm numpy.reshape [] có sẵn trong gói NumPy. Như tên cho thấy, reshape có nghĩa là ‘những thay đổi về hình dạng’. Hàm numpy.reshape [] giúp chúng ta có được một ...

Hướng dẫn dùng np.pi python

Trong bài viết trước tôi đã giới thiệu cho bạn về NumPy, tìm hiểu về Mảng trong NumPy. Trong bài viết này chúng ta sẽ tiếp tục tìm hiểu về các kiểu dữ ...

How to find mode in array python

Mode of a data set is/are the member[s] that occur[s] most frequently in the set. If there are two members that appear most often with same number of times, then the data has two modes. This is ...

Hướng dẫn exponential distribution in python

Hàm exp[x] trong Python trả về ex.Nội dung chínhHow to Generate an Exponential DistributionHow to Calculate Probabilities Using an Exponential DistributionHow to Plot an Exponential ...

Hướng dẫn dùng np.copy python

Nội dung chínhPython Numpy là gì?NumPy ArrayNumpy cài đặt như thế nào?Multi-dimensional numPy array là gì?Python NumPy Array v/s ListPython NumPy OperationsTham khảoPython Numpy là ...

Hướng dẫn change base python

The below provided Python code converts a Python integer to a string in arbitrary base [ from 2 up to infinity ] and works in both directions. So all the created strings can be converted back to ...

Hướng dẫn np sum trong python

Hàm numpy.sum [] có sẵn trong gói NumPy của Python. Hàm này được sử dụng để tính tổng của tất cả các phần tử, tổng của mỗi hàng và tổng của mỗi cột ...

Hướng dẫn slice sparse matrix python

Depending on the indexing, it might be easier to construct the extractor/indexing matrix with the coo style of inputs:In [129]: from scipy import sparse In [130]: M = ...

Hướng dẫn python docstring default value

Write the docstring on the getter, because 1] thats what help[MyClass] shows, and 2] its also how its done in the Python docs -- see the x.setter example.Nội dung chínhAbout docstrings and ...

Hướng dẫn factorial pseudocode in python

View DiscussionImprove ArticleSave ArticleReadDiscussView DiscussionImprove ArticleSave ArticleFactorial of a non-negative integer, is multiplication of all integers smaller than or equal to n. For ...

Hướng dẫn dùng np min python

Numpy [Numeric Python]: là một thư viện toán học phổ biến và mạnh mẽ của Python. Cho phép làm việc hiệu quả với ma trận và mảng, đặc biệt là dữ liệu ...

How does python calculate accuracy?

Balanced accuracy is a metric we can use to assess the performance of a classification model.It is calculated as:Balanced accuracy = [Sensitivity + Specificity] / 2where:Sensitivity: The “true ...

Hướng dẫn dùng np shape python

NumpyNumpy là một thư viện lõi phục vụ cho khoa học máy tính của Python, hỗ trợ cho việc tính toán các mảng nhiều chiều, có kích thước lớn với các hàm đã ...

Chủ Đề