Bạn có thể sử dụng phương thức Series.dt.time
để chuyển đổi gấu trúc TimeStamps
thành đối tượng datetime.time
và sau đó những thứ khác rất đơn giản:
import pandas as pd
import numpy as np
from datetime import time
#Create fake dataset
df = pd.DataFrame[]
df["CRASH TIME"] = pd.to_datetime[['2017-06-26 22:00:00', '2017-06-27 21:50:00']]
df["SUNRISE"] = [time[22, 26, 50, 135154], time[21, 26, 49, 135154]]
print[df.head[]]
print["CRASH TIME element type:", type[df["CRASH TIME"][0]]]
print["SUNRISE element type:", type[df["SUNRISE"][0]]]
df["CRASH TIME"] = df["CRASH TIME"].dt.time
print["CRASH TIME element type:", type[df["CRASH TIME"][0]]]
Output->
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
Pandas Timestamp tương đương với DateTime trong Python. Dấu thời gian được sử dụng cho các cấu trúc dữ liệu định hướng chuỗi thời gian trong gấu trúc. Đôi khi ngày và thời gian được cung cấp dưới dạng dấu thời gian trong gấu trúc hoặc có lợi để được chuyển đổi theo dấu thời gian. Và, cần phải so sánh dấu thời gian để biết mục nhập mới nhất, các mục giữa hai dấu thời gian, mục cũ nhất, v.v. cho các nhiệm vụ khác nhau. So sánh giữa các đối tượng dấu thời gian của gấu trúc được thực hiện bằng cách sử dụng các toán tử so sánh đơn giản:>,
Thời gian cho có thể được chuyển đổi thành Pandas Timestamp bằng phương thức pandas.timestamp []. Phương pháp này có thể lấy đầu vào ở các dạng khác nhau, chẳng hạn như chuỗi giống như DateTime [ví dụ: '2017-01-01T12'], Epoch Unix tính theo đơn vị vài giây [1513393355.5], v.v. giờ, phút, thứ hai, vv được phân tách bằng dấu phẩy hoặc sử dụng tên biến. Ví dụ: nếu chúng ta muốn viết 2018/2/21 11:40:00, chúng ta có thể cung cấp [2018,2,21,11,40] làm tham số cho phương thức Timestamp hoặc có thể viết [năm = 2018, tháng = 2, ngày = 21, giờ = 11, phút = 40]. Các giá trị không được cung cấp sẽ được coi là không. Cách tiếp cận này được sử dụng trong mã sau để tạo cột Timestamp ‘New_time, bằng cách sử dụng thông tin ngày và thời gian được cung cấp.
Approach:
- Tạo một khung dữ liệu với các giá trị ngày và thời gian
- Chuyển đổi giá trị ngày và thời gian thành các giá trị dấu thời gian bằng phương thức pandas.timestamp []
- So sánh dấu thời gian cần thiết bằng cách sử dụng các toán tử so sánh thường xuyên.
Tạo một bản dữ liệu gấu trúc với ngày và giờ:
Python3
import
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
0 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
1 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
2 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
3 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
4 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
5 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
6 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
7 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
8 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
7 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
8 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
7 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
8First entry is old3
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
8First entry is old3
First entry is old6
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
4First entry is old8
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
62017-11-29 10:10:00 2017-11-30 10:20:00 2017-12-31 10:25:00 2018-01-01 11:30:000
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
82017-11-29 10:10:00 2017-11-30 10:20:00 2017-12-31 10:25:00 2018-01-01 11:30:000
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
82017-11-29 10:10:00 2017-11-30 10:20:00 2017-12-31 10:25:00 2018-01-01 11:30:004
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
82017-11-29 10:10:00 2017-11-30 10:20:00 2017-12-31 10:25:00 2018-01-01 11:30:006
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
82017-11-29 10:10:00 2017-11-30 10:20:00 2017-12-31 10:25:00 2018-01-01 11:30:008
First entry is old6
Các
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
4Latest Timestamp: 2018-02-02 11:40:00 Difference: 1 days 00:10:004
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
6Latest Timestamp: 2018-02-02 11:40:00 Difference: 1 days 00:10:006
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
8Latest Timestamp: 2018-02-02 11:40:00 Difference: 1 days 00:10:006
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
8Latest Timestamp: 2018-02-02 11:40:00 Difference: 1 days 00:10:006
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
82017-11-29 10:10:00 2017-11-30 10:20:00 2017-12-31 10:25:00 2018-01-01 11:30:000
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
82017-11-29 10:10:00 2017-11-30 10:20:00 2017-12-31 10:25:00 2018-01-01 11:30:000
First entry is old6
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
4Series.dt.time
7 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
6Latest Timestamp: 2018-02-02 11:40:00 Difference: 1 days 00:10:006
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
8TimeStamps
1 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
8TimeStamps
3 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
80 False 1 False 2 False 3 False 4 True Name: new_time, dtype: bool5
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
8TimeStamps
7TimeStamps
8TimeStamps
9 datetime.time
0
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
4datetime.time
2 datetime.time
3datetime.time
4datetime.time
5
datetime.time
6datetime.time
7datetime.time
8
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
2 import
0import
1import
2__12 import
4import
5import
6
Output:
DF trên được sử dụng trong các ví dụ sau.
Ví dụ 1: Ở đây, dấu thời gian thứ nhất và thứ hai trong ‘new_time, được so sánh để biết những người lâu đời nhất trong số đó. Here, the first and second timestamp in ‘new_time’ are compared to know the oldest among those.
Python3
Is
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
4 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
10 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
11 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
12import
5 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
14 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
15 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
4 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
10 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
11 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
19import
5Output:
First entry is old
Ví dụ 2: Ở đây, tất cả các dấu thời gian trong ‘new_time, được so sánh với dấu thời gian [2018-01-05 12:00:00] và các mục trước khi dấu thời gian này được trả về Here, all timestamps in ‘new_time’ are compared with Timestamp[2018-01-05 12:00:00] and the entries before this timestamp are returned
Python3
Is
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
4import
7 datetime.time
6datetime.time
7 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
34First entry is old3
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
82017-11-29 10:10:00 2017-11-30 10:20:00 2017-12-31 10:25:00 2018-01-01 11:30:006
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
8__ CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
43 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
10 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
27datetime.time
7 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
47Output:
2017-11-29 10:10:00 2017-11-30 10:20:00 2017-12-31 10:25:00 2018-01-01 11:30:00
Ví dụ 3: Ở đây một lần nữa chúng tôi so sánh tất cả các dấu thời gian với dấu thời gian [2018-01-05 12:00:00], nhưng đã trả về kết quả so sánh dưới dạng giá trị boolean [true/false] cho tất cả các dấu thời gian. Here again we compared all timestamps with Timestamp[2018-01-05 12:00:00], but returned comparison result as boolean values [True/False] for all timestamps.
Python3
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
10 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
27datetime.time
7 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
51First entry is old3
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
82017-11-29 10:10:00 2017-11-30 10:20:00 2017-12-31 10:25:00 2018-01-01 11:30:006
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
8 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
39 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
82017-11-29 10:10:00 2017-11-30 10:20:00 2017-12-31 10:25:00 2018-01-01 11:30:004
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
59Output:
0 False 1 False 2 False 3 False 4 True Name: new_time, dtype: bool
Ví dụ 4: Ở đây, hàm tối đa được sử dụng để có được tối đa của tất cả các dấu thời gian, đó là mục gần đây trong cột ‘New_time. Here, the max function is used to get the maximum of all timestamps, that is the recent entry in the ‘new_time’ column.
Ngoài ra, với điều đó, chúng tôi đã tính toán chênh lệch thời gian giữa dấu thời gian thứ nhất và lần thứ hai trong cột ‘New_time.
Python3
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
10 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
11 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
62 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
8 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
64 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
27datetime.time
7 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
67Is
CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
10 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
11 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
84 CRASH TIME SUNRISE
0 2017-06-26 22:00:00 22:26:50.135154
1 2017-06-27 21:50:00 21:26:49.135154
CRASH TIME element type:
SUNRISE element type:
CRASH TIME element type:
85Output:
Latest Timestamp: 2018-02-02 11:40:00 Difference: 1 days 00:10:00