Ngày tháng trong Python không phải là kiểu dữ liệu của riêng nó, nhưng chúng ta có thể nhập một mô-đun có tên
hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
0 để làm việc với ngày tháng dưới dạng đối tượng ngày thángVí dụ
Nhập mô-đun datetime và hiển thị ngày hiện tại
nhập ngày giờ
x = ngày giờ. ngày giờ. bây giờ[]
in[x]
Xuất ngày
Khi chúng tôi thực thi mã từ ví dụ trên, kết quả sẽ là
Ngày chứa năm, tháng, ngày, giờ, phút, giây và micro giây
Mô-đun
hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
0 có nhiều phương thức để trả về thông tin về đối tượng ngày thángDưới đây là một vài ví dụ, bạn sẽ tìm hiểu thêm về chúng ở phần sau của chương này
Ví dụ
Trả về năm và tên của ngày trong tuần
nhập ngày giờ
x = ngày giờ. ngày giờ. hiện nay[]
in[x. năm]
in[x. strftime["%A"]]
Tạo đối tượng ngày
Để tạo ngày, chúng ta có thể sử dụng lớp
hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
2 [hàm tạo] của mô-đun hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
0Lớp
hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
2 yêu cầu ba tham số để tạo ngày. năm tháng ngàyVí dụ
Tạo một đối tượng ngày
nhập ngày giờ
x = ngày giờ. ngày giờ[2020, 5, 17]
in[x]
Tự mình thử »Lớp
hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
2 cũng nhận các tham số về thời gian và múi giờ [giờ, phút, giây, micro giây, tzone], nhưng chúng là tùy chọn và có giá trị mặc định là hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
6, [hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
7 cho múi giờ]Phương thức strftime[]
Đối tượng
hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
0 có phương thức định dạng đối tượng ngày tháng thành các chuỗi có thể đọc đượcPhương thức này được gọi là
hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
9 và nhận một tham số, hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
00, để chỉ định định dạng của chuỗi được trả vềChương trình Python để in năm, tháng và ngày hiện tại
Cải thiện bài viết
Lưu bài viết
Thích bài viết
- Độ khó. Cơ bản
- Cập nhật lần cuối. 29 tháng 12 năm 2020
Cải thiện bài viết
Lưu bài viết
Trong bài viết này, nhiệm vụ là viết Chương trình Python để in năm, tháng, ngày hiện tại
Tiếp cận
- Trong Python, để in ra ngày hiện tại bao gồm năm, tháng, ngày, nó có một module tên là datetime. Từ mô-đun DateTime, nhập lớp ngày
- Tạo một đối tượng của lớp ngày
- Gọi hàm today[ ] của lớp date để lấy ngày hôm nay
- Bằng cách sử dụng đối tượng được tạo, chúng ta có thể in năm, tháng, ngày [thuộc tính của lớp ngày] của ngày hôm nay
Python3
[6, 59, 51] # Your output should look like this1
[6, 59, 51] # Your output should look like this2
hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
0[6, 59, 51] # Your output should look like this4
[6, 59, 51] # Your output should look like this5
[6, 59, 51] # Your output should look like this6
[6, 59, 51] # Your output should look like this7
[6, 59, 51] # Your output should look like this8
[6, 59, 51] # Your output should look like this9
[6, 59, 51] # Your output should look like this10
[6, 59, 51] # Your output should look like this6
[6, 59, 51] # Your output should look like this12
[6, 59, 51] # Your output should look like this13
[6, 59, 51] # Your output should look like this14
[6, 59, 51] # Your output should look like this15
[6, 59, 51] # Your output should look like this16
[6, 59, 51] # Your output should look like this6
[6, 59, 51] # Your output should look like this18
[6, 59, 51] # Your output should look like this13
[6, 59, 51] # Your output should look like this14
[6, 59, 51] # Your output should look like this21
[6, 59, 51] # Your output should look like this22
[6, 59, 51] # Your output should look like this13
[6, 59, 51] # Your output should look like this14
[6, 59, 51] # Your output should look like this25
[6, 59, 51] # Your output should look like this26
[6, 59, 51] # Your output should look like this13
[6, 59, 51] # Your output should look like this14
[6, 59, 51] # Your output should look like this29
hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
00đầu ra
Current date: 2020-12-10 Current year: 2020 Current month: 12 Current day: 10
Ghi chú cá nhân của tôi arrow_drop_up
Tiết kiệm
Vui lòng Đăng nhập để nhận xét.
Trích xuất dữ liệu ở định dạng ngày giờ có thể rất phức tạp và khó chịu. Mình đã tổng hợp một vài điều về datetime trong Python mà mình học được ở đoạn code bên dưới
Hãy tải các gói mà chúng ta sẽ cần, chẳng hạn như
date: 2021-02-17 date of the month: 17 day of the week: Wednesday month: 2 month name: February year: 20211 và
hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
0, sau đó, xác định một biến để lưu trữ định dạng dữ liệu ngày giờimport pandas as pd
from datetime import datetime
my_datetime = "2021-02-17 06:59:51"
Trích xuất thời gian [giờ, phút, giây] từ một chuỗi
Để trích xuất giờ, phút và giây, nó khá đơn giản
hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
[6, 59, 51] # Your output should look like this
Trích xuất ngày từ một chuỗi
Bây giờ bạn có thể hiểu rằng để trích xuất thành phần ngày giờ trong một biến, bạn có thể sử dụng
hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
0. Bạn có thể sử dụng cùng một thứ để trích xuất ngày, tháng, nămdate = pd.to_datetime[my_datetime].date[]
day = pd.to_datetime[my_datetime].day
day_name = pd.to_datetime[my_datetime].day_name[]
month = pd.to_datetime[my_datetime].month
month_name = pd.to_datetime[my_datetime].month_name[]
year = pd.to_datetime[my_datetime].year
print["date: " + str[date]]
print["date of the month: " + str[day]]
print["day of the week: " + day_name]
print["month: " + str[month]]
print["month name: " + month_name]
print["year: " + str[year]]
date: 2021-02-17 date of the month: 17 day of the week: Wednesday month: 2 month name: February year: 2021
Trích xuất ngày từ cột khung dữ liệu
Bây giờ nếu dữ liệu của bạn nằm trong cột khung dữ liệu thì sao? . Vì vậy, đây là những gì bạn nên sử dụng để thay thế, nhưng trước tiên hãy xây dựng khung dữ liệu trước. Cột trình tự chỉ là thứ gì đó ngẫu nhiên mà tôi nghĩ ra để khung dữ liệu có thể có nhiều cột
df = pd.DataFrame[{"sequence":[100, 200, 300],
"datetime":["2021-02-17 06:59:59",
"2021-01-15 02:19:59",
"2021-01-02 10:15:18"]}]
df
Đây là mã để trích xuất tất cả thông tin. Lưu ý
hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
1 được sử dụng ở đây. Dòng mã đầu tiên chỉ được sử dụng để trích xuất ngày, trong khi dòng mã thứ hai chỉ cho bạn cách trích xuất tháng và nămdf['date'] = pd.to_datetime[df['datetime']].dt.date
df['month'] = pd.to_datetime[df['datetime']].dt.to_period['M']
print[df['date']]
print[df['month']]
Trích xuất thời gian bằng Strftime
Bạn cũng có thể làm nhiều hơn nữa bằng cách sử dụng
hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
2Cẩn thận với cách viết hoa. Ví dụ bên dưới sử dụng
hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
3 để lấy tên tháng, tất cả đều ở định dạng rút gọn [hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
4, v.v.]. Nếu bạn sử dụng hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
5, bạn sẽ nhận được tên của cả tháng [hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
6, v.v.]. Ngoài ra, hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
7 sẽ cung cấp cho bạn ngày đầy đủ [hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
8] thay vì chỉ ngày [hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
9]. Chơi xung quanh với những thứ này để hiểu thêm về định dạng hour = pd.to_datetime[my_datetime].hour
minute = pd.to_datetime[my_datetime].minute
second = pd.to_datetime[my_datetime].second
print[hour]
print[minute]
print[second]
2import pandas as pd
from datetime import datetime
my_datetime = "2021-02-17 06:59:51"
9Nếu bạn muốn tìm hiểu thêm về điều này, bạn có thể đọc tài liệu chính thức về datetime của Python bằng liên kết tại đây