Python có thể nhận ra ngày không?

Khi gửi datatables tới pandas dataframes (Python), các giá trị số (datetimes) được gửi dưới dạng chuỗi

Tôi không mong đợi hành vi này, vì bây giờ tôi phải xử lý việc này theo cách thủ công (xem xét thời gian biểu trong Python và JMP là khác nhau)

Bất kỳ giải pháp thay thế?

Trên trang này, bạn sẽ tìm hiểu cách xử lý ngày tháng bằng cách sử dụng đối tượng

Downloading from https://ndownloader.figshare.com/files/12948515
4 trong Python với gấu trúc, sử dụng bộ dữ liệu về nhiệt độ hàng ngày (tối đa tính bằng độ F) và tổng lượng mưa (inch) vào tháng 7 năm 2018 cho Boulder, CO, do National Oceanic cung cấp

Nhập gói và lấy dữ liệu

Để bắt đầu, hãy nhập các gói cần thiết để hoạt động với pandas dataframe và tải xuống dữ liệu. Bạn sẽ làm việc với các mô-đun từ pandas và matplotlib để vẽ biểu đồ ngày hiệu quả hơn và bạn sẽ làm việc với gói seaborn để tạo ra các biểu đồ hấp dẫn hơn

# Import necessary packages
import os
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import earthpy as et

# Handle date time conversions between pandas and matplotlib
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

# Use white grid plot background from seaborn
sns.set(font_scale=1.5, style="whitegrid")

# Download csv of temp (F) and precip (inches) in July 2018 for Boulder, CO
file_url = "https://ndownloader.figshare.com/files/12948515"
et.data.get_data(url=file_url)

# Set working directory
os.chdir(os.path.join(et.io.HOME, 'earth-analytics'))

# Define relative path to file
file_path = os.path.join("data", "earthpy-downloads",
                         "july-2018-temperature-precip.csv")

# Import file into pandas dataframe
boulder_july_2018 = pd.read_csv(file_path)

Downloading from https://ndownloader.figshare.com/files/12948515

# Display first few rows
boulder_july_2018.head()

datemax_tempprecip02018-07-01870. 0012018-07-02920. 0022018-07-0390-999. 0032018-07-04870. 0042018-07-05840. 24

# View dataframe info
boulder_july_2018.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 31 entries, 0 to 30
Data columns (total 3 columns):
 #   Column    Non-Null Count  Dtype  
---  ------    --------------  -----  
 0   date      31 non-null     object 
 1   max_temp  31 non-null     int64  
 2   precip    31 non-null     float64
dtypes: float64(1), int64(1), object(1)
memory usage: 872.0+ bytes

Xem các loại dữ liệu trong Pandas Dataframes

Bây giờ bạn đã nhập dữ liệu vào khung dữ liệu gấu trúc, bạn có thể truy vấn các loại dữ liệu của các cột trong khung dữ liệu bằng thuộc tính khung dữ liệu

Downloading from https://ndownloader.figshare.com/files/12948515
8

________số 8

date         object
max_temp      int64
precip      float64
dtype: object

Thuộc tính

Downloading from https://ndownloader.figshare.com/files/12948515
8 chỉ ra rằng các cột dữ liệu trong khung dữ liệu gấu trúc của bạn được lưu trữ dưới dạng một số loại dữ liệu khác nhau như sau

  • ngày làm đối tượng. Chuỗi ký tự nằm trong dấu ngoặc kép
  • max_temp dưới dạng số nguyên 64 bit int64. Đây là một giá trị số sẽ không bao giờ chứa dấu thập phân
  • kết tủa dưới dạng float64 - float 64 bit. Loại dữ liệu này chấp nhận dữ liệu có nhiều định dạng số bao gồm số thập phân (giá trị dấu phẩy động) và số nguyên. Numeric cũng chấp nhận số lớn hơn int sẽ

Điều tra thêm loại dữ liệu trong cột

# Display first few rows
boulder_july_2018.head()
0 để xem loại dữ liệu hoặc loại thông tin chứa trong đó

# Import necessary packages
import os
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import earthpy as et

# Handle date time conversions between pandas and matplotlib
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

# Use white grid plot background from seaborn
sns.set(font_scale=1.5, style="whitegrid")
2

# Import necessary packages
import os
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import earthpy as et

# Handle date time conversions between pandas and matplotlib
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

# Use white grid plot background from seaborn
sns.set(font_scale=1.5, style="whitegrid")
3

Lưu ý rằng trong khi bạn có thể thấy cột này là ngày tháng, thì Python lưu trữ các giá trị dưới dạng kiểu

# Display first few rows
boulder_july_2018.head()
1 hoặc chuỗi

Vẽ ngày dưới dạng chuỗi

Để hiểu tại sao việc sử dụng các đối tượng

Downloading from https://ndownloader.figshare.com/files/12948515
4 có thể giúp bạn tạo các biểu đồ tốt hơn, hãy bắt đầu bằng cách tạo một biểu đồ tiêu chuẩn bằng matplotlib, dựa trên cột
# Display first few rows
boulder_july_2018.head()
0 (dưới dạng chuỗi) và cột
# Display first few rows
boulder_july_2018.head()
4

# Download csv of temp (F) and precip (inches) in July 2018 for Boulder, CO
file_url = "https://ndownloader.figshare.com/files/12948515"
et.data.get_data(url=file_url)

# Set working directory
os.chdir(os.path.join(et.io.HOME, 'earth-analytics'))

# Define relative path to file
file_path = os.path.join("data", "earthpy-downloads",
                         "july-2018-temperature-precip.csv")

# Import file into pandas dataframe
boulder_july_2018 = pd.read_csv(file_path)
0

Python có thể nhận ra ngày không?
Biểu đồ đường của lượng mưa ở Boulder, CO với ngày ở dạng chuỗi và không xóa giá trị không có dữ liệu

Trước tiên, bạn sẽ nhận thấy rằng có nhiều giá trị âm trong tập dữ liệu này - đây thực sự là những giá trị “không có dữ liệu” mà bạn sẽ xử lý trong phần sau của trang này

Bây giờ, hãy nhìn kỹ ngày tháng trên trục x. Khi bạn vẽ một trường chuỗi cho trục x,

# Display first few rows
boulder_july_2018.head()
5 gặp khó khăn khi cố vẽ tất cả các nhãn ngày. Mỗi giá trị được đọc dưới dạng một chuỗi và rất khó để cố gắng khớp tất cả các giá trị đó trên trục x một cách hiệu quả

Bạn có thể tránh sự cố này bằng cách chuyển đổi ngày từ chuỗi thành đối tượng

Downloading from https://ndownloader.figshare.com/files/12948515
4 trong quá trình nhập dữ liệu vào khung dữ liệu gấu trúc. Sau khi các ngày được chuyển đổi thành một đối tượng
Downloading from https://ndownloader.figshare.com/files/12948515
4, bạn có thể dễ dàng tùy chỉnh các ngày trên cốt truyện của mình, dẫn đến một cốt truyện hấp dẫn hơn về mặt hình ảnh

Nhập cột ngày vào Pandas Dataframe dưới dạng đối tượng ngày giờ

Để nhập ngày tháng dưới dạng đối tượng

Downloading from https://ndownloader.figshare.com/files/12948515
4, bạn có thể sử dụng tham số
# Display first few rows
boulder_july_2018.head()
9 của hàm
# View dataframe info
boulder_july_2018.info()
0 cho phép bạn chỉ ra rằng một cột cụ thể sẽ được chuyển đổi thành đối tượng
Downloading from https://ndownloader.figshare.com/files/12948515
4

# View dataframe info
boulder_july_2018.info()
2

Nếu bạn có một cột duy nhất chứa ngày trong dữ liệu của mình, bạn cũng có thể đặt ngày làm chỉ mục cho khung dữ liệu bằng cách sử dụng tham số

# View dataframe info
boulder_july_2018.info()
3

# View dataframe info
boulder_july_2018.info()
4

Bạn sẽ sử dụng chỉ mục này trong các bài học sau để cho phép bạn nhanh chóng tóm tắt và tổng hợp dữ liệu của mình theo ngày

Giờ đây, bạn có thể tạo lại khung dữ liệu bằng cách sử dụng tham số

# Display first few rows
boulder_july_2018.head()
9 để chuyển đổi ngày từ chuỗi thành giá trị
Downloading from https://ndownloader.figshare.com/files/12948515
4 và
# View dataframe info
boulder_july_2018.info()
3 để đặt chỉ mục của khung dữ liệu thành giá trị
Downloading from https://ndownloader.figshare.com/files/12948515
4

# Download csv of temp (F) and precip (inches) in July 2018 for Boulder, CO
file_url = "https://ndownloader.figshare.com/files/12948515"
et.data.get_data(url=file_url)

# Set working directory
os.chdir(os.path.join(et.io.HOME, 'earth-analytics'))

# Define relative path to file
file_path = os.path.join("data", "earthpy-downloads",
                         "july-2018-temperature-precip.csv")

# Import file into pandas dataframe
boulder_july_2018 = pd.read_csv(file_path)
1

max_tempprecipdate2018-07-01870. 002018-07-02920. 002018-07-0390-999. 002018-07-04870. 002018-07-05840. 24

Lưu ý rằng trong quá trình nhập, bạn đã chuyển đổi cột

# Display first few rows
boulder_july_2018.head()
0 thành loại
Downloading from https://ndownloader.figshare.com/files/12948515
4 và bạn cũng đặt chỉ mục của khung dữ liệu là đối tượng
Downloading from https://ndownloader.figshare.com/files/12948515
4 đó

Vì vậy, thay vì chỉ mục là

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 31 entries, 0 to 30
Data columns (total 3 columns):
 #   Column    Non-Null Count  Dtype  
---  ------    --------------  -----  
 0   date      31 non-null     object 
 1   max_temp  31 non-null     int64  
 2   precip    31 non-null     float64
dtypes: float64(1), int64(1), object(1)
memory usage: 872.0+ bytes
2 ban đầu của các giá trị từ 0 đến 31, chỉ mục hiện là một
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 31 entries, 0 to 30
Data columns (total 3 columns):
 #   Column    Non-Null Count  Dtype  
---  ------    --------------  -----  
 0   date      31 non-null     object 
 1   max_temp  31 non-null     int64  
 2   precip    31 non-null     float64
dtypes: float64(1), int64(1), object(1)
memory usage: 872.0+ bytes
3

# View dataframe info
boulder_july_2018.info()

# Download csv of temp (F) and precip (inches) in July 2018 for Boulder, CO
file_url = "https://ndownloader.figshare.com/files/12948515"
et.data.get_data(url=file_url)

# Set working directory
os.chdir(os.path.join(et.io.HOME, 'earth-analytics'))

# Define relative path to file
file_path = os.path.join("data", "earthpy-downloads",
                         "july-2018-temperature-precip.csv")

# Import file into pandas dataframe
boulder_july_2018 = pd.read_csv(file_path)
3

Nhớ lại rằng sau khi bạn đặt chỉ mục từ một cột trong khung dữ liệu, cột ban đầu đó không còn được liệt kê dưới dạng cột nữa mà được xác định là chỉ mục của khung dữ liệu

________số 8

# Download csv of temp (F) and precip (inches) in July 2018 for Boulder, CO
file_url = "https://ndownloader.figshare.com/files/12948515"
et.data.get_data(url=file_url)

# Set working directory
os.chdir(os.path.join(et.io.HOME, 'earth-analytics'))

# Define relative path to file
file_path = os.path.join("data", "earthpy-downloads",
                         "july-2018-temperature-precip.csv")

# Import file into pandas dataframe
boulder_july_2018 = pd.read_csv(file_path)
5

Đừng lo lắng - dữ liệu vẫn còn đó. Để kiểm tra các giá trị của chỉ mục của khung dữ liệu, bạn có thể sử dụng thuộc tính

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 31 entries, 0 to 30
Data columns (total 3 columns):
 #   Column    Non-Null Count  Dtype  
---  ------    --------------  -----  
 0   date      31 non-null     object 
 1   max_temp  31 non-null     int64  
 2   precip    31 non-null     float64
dtypes: float64(1), int64(1), object(1)
memory usage: 872.0+ bytes
4

# Download csv of temp (F) and precip (inches) in July 2018 for Boulder, CO
file_url = "https://ndownloader.figshare.com/files/12948515"
et.data.get_data(url=file_url)

# Set working directory
os.chdir(os.path.join(et.io.HOME, 'earth-analytics'))

# Define relative path to file
file_path = os.path.join("data", "earthpy-downloads",
                         "july-2018-temperature-precip.csv")

# Import file into pandas dataframe
boulder_july_2018 = pd.read_csv(file_path)
6

# Download csv of temp (F) and precip (inches) in July 2018 for Boulder, CO
file_url = "https://ndownloader.figshare.com/files/12948515"
et.data.get_data(url=file_url)

# Set working directory
os.chdir(os.path.join(et.io.HOME, 'earth-analytics'))

# Define relative path to file
file_path = os.path.join("data", "earthpy-downloads",
                         "july-2018-temperature-precip.csv")

# Import file into pandas dataframe
boulder_july_2018 = pd.read_csv(file_path)
7

Vẽ ngày tháng từ Pandas Dataframe Sử dụng Datetime

Trong matplotlib, có một số khác biệt nhỏ về cách đọc biểu đồ thanh và biểu đồ phân tán trong dữ liệu so với cách biểu đồ đường đọc trong dữ liệu

Khi vẽ đồ thị với

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 31 entries, 0 to 30
Data columns (total 3 columns):
 #   Column    Non-Null Count  Dtype  
---  ------    --------------  -----  
 0   date      31 non-null     object 
 1   max_temp  31 non-null     int64  
 2   precip    31 non-null     float64
dtypes: float64(1), int64(1), object(1)
memory usage: 872.0+ bytes
5 và
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 31 entries, 0 to 30
Data columns (total 3 columns):
 #   Column    Non-Null Count  Dtype  
---  ------    --------------  -----  
 0   date      31 non-null     object 
 1   max_temp  31 non-null     int64  
 2   precip    31 non-null     float64
dtypes: float64(1), int64(1), object(1)
memory usage: 872.0+ bytes
6, numpy được sử dụng để nối (một từ ưa thích để kết hợp) một mảng đã được tạo và chuyển vào cho dữ liệu
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 31 entries, 0 to 30
Data columns (total 3 columns):
 #   Column    Non-Null Count  Dtype  
---  ------    --------------  -----  
 0   date      31 non-null     object 
 1   max_temp  31 non-null     int64  
 2   precip    31 non-null     float64
dtypes: float64(1), int64(1), object(1)
memory usage: 872.0+ bytes
7 và/hoặc
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 31 entries, 0 to 30
Data columns (total 3 columns):
 #   Column    Non-Null Count  Dtype  
---  ------    --------------  -----  
 0   date      31 non-null     object 
 1   max_temp  31 non-null     int64  
 2   precip    31 non-null     float64
dtypes: float64(1), int64(1), object(1)
memory usage: 872.0+ bytes
8. Tuy nhiên, numpy không thể nối đối tượng
Downloading from https://ndownloader.figshare.com/files/12948515
4 với các giá trị khác

Do đó, nếu bạn cố gắng chuyển trực tiếp cột hoặc chỉ mục của

Downloading from https://ndownloader.figshare.com/files/12948515
4 sang
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 31 entries, 0 to 30
Data columns (total 3 columns):
 #   Column    Non-Null Count  Dtype  
---  ------    --------------  -----  
 0   date      31 non-null     object 
 1   max_temp  31 non-null     int64  
 2   precip    31 non-null     float64
dtypes: float64(1), int64(1), object(1)
memory usage: 872.0+ bytes
5 hoặc
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 31 entries, 0 to 30
Data columns (total 3 columns):
 #   Column    Non-Null Count  Dtype  
---  ------    --------------  -----  
 0   date      31 non-null     object 
 1   max_temp  31 non-null     int64  
 2   precip    31 non-null     float64
dtypes: float64(1), int64(1), object(1)
memory usage: 872.0+ bytes
6, bạn sẽ nhận được thông báo lỗi

Sử dụng thuộc tính giá trị chỉ mục để vẽ biểu đồ ngày giờ

Để tránh lỗi này, bạn có thể gọi thuộc tính

# View column data types
boulder_july_2018.dtypes
3 trên chỉ mục
Downloading from https://ndownloader.figshare.com/files/12948515
4 bằng cách sử dụng

# View column data types
boulder_july_2018.dtypes
5

Lưu ý rằng ở đây bạn sử dụng

# View column data types
boulder_july_2018.dtypes
6 để truy cập cột ngày giờ vì bạn đã chỉ định cột ngày của mình làm chỉ mục cho khung dữ liệu

Ngoài ra, hãy lưu ý rằng khoảng cách trên trục x trông đẹp hơn và nhãn ngày tháng trục x của bạn dễ đọc hơn, vì Python biết cách chỉ hiển thị các giá trị gia tăng bằng cách sử dụng

Downloading from https://ndownloader.figshare.com/files/12948515
4, thay vì vẽ đồ thị cho từng giá trị ngày tháng

# Download csv of temp (F) and precip (inches) in July 2018 for Boulder, CO
file_url = "https://ndownloader.figshare.com/files/12948515"
et.data.get_data(url=file_url)

# Set working directory
os.chdir(os.path.join(et.io.HOME, 'earth-analytics'))

# Define relative path to file
file_path = os.path.join("data", "earthpy-downloads",
                         "july-2018-temperature-precip.csv")

# Import file into pandas dataframe
boulder_july_2018 = pd.read_csv(file_path)
8

Python có thể nhận ra ngày không?
Biểu đồ lượng mưa phân tán với ngày trục x được định dạng là ngày giờ

# Download csv of temp (F) and precip (inches) in July 2018 for Boulder, CO
file_url = "https://ndownloader.figshare.com/files/12948515"
et.data.get_data(url=file_url)

# Set working directory
os.chdir(os.path.join(et.io.HOME, 'earth-analytics'))

# Define relative path to file
file_path = os.path.join("data", "earthpy-downloads",
                         "july-2018-temperature-precip.csv")

# Import file into pandas dataframe
boulder_july_2018 = pd.read_csv(file_path)
9

Python có thể nhận ra ngày không?
Biểu đồ thanh hiển thị lượng mưa hàng ngày với ngày trục x là ngày giờ

Ghi chú. bạn không cần sử dụng

# View column data types
boulder_july_2018.dtypes
3 khi sử dụng chỉ mục chứa giá trị
# View column data types
boulder_july_2018.dtypes
9, thay vì đối tượng
Downloading from https://ndownloader.figshare.com/files/12948515
4, cũng như khi tạo biểu đồ đường bằng cách sử dụng
date         object
max_temp      int64
precip      float64
dtype: object
1

Tuy nhiên, để đảm bảo tính nhất quán của mã, các ví dụ về biểu đồ trong chương này sẽ sử dụng

date         object
max_temp      int64
precip      float64
dtype: object
2 để tạo tất cả các biểu đồ bằng chỉ mục

Ở các trang sau của chương này, bạn sẽ học cách tùy chỉnh nhiều hơn các nhãn ngày tháng, chẳng hạn như chỉ hiển thị giá trị tháng và ngày hoặc chỉ định khoảng cách cho khoảng thời gian (i. e. tần số)

Làm việc không có giá trị dữ liệu trong Pandas Dataframe

Như đã đề cập trước đây, bạn có thể nhận thấy rằng có nhiều giá trị âm trong tập dữ liệu này, thực ra là giá trị “không có dữ liệu”

Đôi khi dữ liệu bị thiếu trong một tệp do lỗi trong quá trình thu thập, không thể ghi điểm dữ liệu hoặc các lý do khác. Ví dụ: hãy tưởng tượng một bảng tính có các ô trống. Nếu các ô trống, bạn không biết chắc liệu những dữ liệu đó không được thu thập hay liệu ai đó đã quên điền chúng vào

Để giải thích dữ liệu bị thiếu (không phải do nhầm lẫn), bạn có thể đặt một giá trị vào các ô biểu thị “không có dữ liệu” để làm rõ rằng những dữ liệu này không thể sử dụng được để phân tích hoặc vẽ biểu đồ

Thông thường, bạn sẽ tìm thấy tập dữ liệu sử dụng một giá trị cụ thể cho “không có dữ liệu”. Trong nhiều ngành khoa học, giá trị

date         object
max_temp      int64
precip      float64
dtype: object
3 thường được sử dụng để biểu thị các giá trị “không có dữ liệu”

Trong ví dụ trên trang này, dữ liệu trong

date         object
max_temp      int64
precip      float64
dtype: object
4 chứa các giá trị "không có dữ liệu" trong cột
date         object
max_temp      int64
precip      float64
dtype: object
5 sử dụng giá trị
date         object
max_temp      int64
precip      float64
dtype: object
3

Nếu bạn không chỉ định rằng giá trị

date         object
max_temp      int64
precip      float64
dtype: object
3 là giá trị “không có dữ liệu”, thì các giá trị này sẽ được nhập dưới dạng dữ liệu thực, điều này sẽ ảnh hưởng đến mọi số liệu thống kê, phép tính và biểu đồ sử dụng dữ liệu

Ví dụ: khám phá dữ liệu bằng cách sử dụng

date         object
max_temp      int64
precip      float64
dtype: object
8 để xem tác động của việc đọc các giá trị
date         object
max_temp      int64
precip      float64
dtype: object
3 đó dưới dạng giá trị thực

Downloading from https://ndownloader.figshare.com/files/12948515
0

max_tempprecipcount31. 00000031. 000000mean88. 129032-96. 618065std6. 626925300. 256388min75. 000000-999. 00000025%84. 0000000. 00000050%88. 0000000. 00000075%94. 0000000. 050000max97. 0000000. 450000

Các giá trị

date         object
max_temp      int64
precip      float64
dtype: object
3 đã được nhập dưới dạng giá trị số vào khung dữ liệu gấu trúc khi nó được tạo và do đó, các giá trị này được đưa vào thống kê tóm tắt

Để đảm bảo các giá trị "không có dữ liệu" được bỏ qua đúng cách trong thống kê tóm tắt của bạn, bạn có thể chỉ định giá trị "không có dữ liệu" trong quá trình nhập để các giá trị đó không được đọc dưới dạng giá trị số thực bằng cách sử dụng tham số

# Import necessary packages
import os
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import earthpy as et

# Handle date time conversions between pandas and matplotlib
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

# Use white grid plot background from seaborn
sns.set(font_scale=1.5, style="whitegrid")
21 như sau

# Import necessary packages
import os
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import earthpy as et

# Handle date time conversions between pandas and matplotlib
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

# Use white grid plot background from seaborn
sns.set(font_scale=1.5, style="whitegrid")
22

Ví dụ

# Import necessary packages
import os
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import earthpy as et

# Handle date time conversions between pandas and matplotlib
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

# Use white grid plot background from seaborn
sns.set(font_scale=1.5, style="whitegrid")
23

Điều này sẽ yêu cầu gấu trúc coi tất cả các giá trị của

date         object
max_temp      int64
precip      float64
dtype: object
3 là không có dữ liệu và do đó, không đưa chúng vào phân tích hoặc trên biểu đồ

Bây giờ bạn đã biết cách xử lý các giá trị “không có dữ liệu”, hãy tạo lại khung dữ liệu một lần nữa bằng cách đưa vào một giá trị cho tham số

# Import necessary packages
import os
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import earthpy as et

# Handle date time conversions between pandas and matplotlib
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

# Use white grid plot background from seaborn
sns.set(font_scale=1.5, style="whitegrid")
21

Downloading from https://ndownloader.figshare.com/files/12948515
1

max_tempprecipdate2018-07-01870. 002018-07-02920. 002018-07-0390NaN2018-07-04870. 002018-07-05840. 24

Lưu ý rằng giá trị

date         object
max_temp      int64
precip      float64
dtype: object
3 cho
date         object
max_temp      int64
precip      float64
dtype: object
5 vào ngày 3 tháng 7 đã được thay thế bằng
Downloading from https://ndownloader.figshare.com/files/12948515
6 để biểu thị giá trị "không có dữ liệu"

Bây giờ hãy xem số liệu thống kê tóm tắt

Downloading from https://ndownloader.figshare.com/files/12948515
2

max_tempprecipcount31. 00000028. 000000mean88. 1290320. 065714std6. 6269250. 120936min75. 0000000. 00000025%84. 0000000. 00000050%88. 0000000. 00000075%94. 0000000. 055000max97. 0000000. 450000

Cuối cùng, vẽ biểu đồ dữ liệu lần cuối để thấy rằng các giá trị âm

date         object
max_temp      int64
precip      float64
dtype: object
3 không còn trên biểu đồ

# Download csv of temp (F) and precip (inches) in July 2018 for Boulder, CO
file_url = "https://ndownloader.figshare.com/files/12948515"
et.data.get_data(url=file_url)

# Set working directory
os.chdir(os.path.join(et.io.HOME, 'earth-analytics'))

# Define relative path to file
file_path = os.path.join("data", "earthpy-downloads",
                         "july-2018-temperature-precip.csv")

# Import file into pandas dataframe
boulder_july_2018 = pd.read_csv(file_path)
8

Python có thể nhận ra ngày không?
Biểu đồ phân tán hiển thị lượng mưa hàng ngày với các ngày trên trục x được làm sạch và loại bỏ các giá trị không có dữ liệu

Bằng cách sử dụng tham số

# Import necessary packages
import os
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import earthpy as et

# Handle date time conversions between pandas and matplotlib
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

# Use white grid plot background from seaborn
sns.set(font_scale=1.5, style="whitegrid")
21, bạn đã yêu cầu Python bỏ qua các giá trị “không có dữ liệu” đó (hiện được gắn nhãn là
Downloading from https://ndownloader.figshare.com/files/12948515
6) khi nó thực hiện các phép tính trên dữ liệu và khi nó vẽ biểu đồ dữ liệu

Ghi chú. nếu có nhiều loại giá trị bị thiếu trong tập dữ liệu của bạn, bạn có thể thêm nhiều giá trị vào tham số

# Import necessary packages
import os
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import earthpy as et

# Handle date time conversions between pandas and matplotlib
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

# Use white grid plot background from seaborn
sns.set(font_scale=1.5, style="whitegrid")
21 như sau

# Import necessary packages
import os
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import earthpy as et

# Handle date time conversions between pandas and matplotlib
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

# Use white grid plot background from seaborn
sns.set(font_scale=1.5, style="whitegrid")
33

sẽ chỉ định rằng các giá trị “không có dữ liệu” là “NA” dưới dạng chuỗi, khoảng trống dưới dạng chuỗi hoặc giá trị số

date         object
max_temp      int64
precip      float64
dtype: object
3

Bây giờ bạn đã biết cách nhập ngày vào khung dữ liệu gấu trúc bằng cách sử dụng đối tượng

Downloading from https://ndownloader.figshare.com/files/12948515
4 trong Python và cách xử lý khi không có giá trị dữ liệu

Trong các trang tiếp theo của chương này, bạn sẽ tìm hiểu thêm về cách làm việc với

Downloading from https://ndownloader.figshare.com/files/12948515
4 trong Python để tập hợp con và lấy mẫu lại dữ liệu theo thời gian cũng như tùy chỉnh các biểu đồ của bạn hơn nữa

Python có nhận ra ngày không?

Rất may, Python có mô-đun datetime cho phép chúng ta dễ dàng thao tác với các đối tượng đại diện cho ngày và giờ . Trong hướng dẫn ngày giờ Python này, chúng ta sẽ tìm hiểu những điều sau. Công dụng của mô-đun datetime trong Python.

Làm cách nào để phát hiện ngày trong Python?

Ví dụ 1. Python lấy ngày hôm nay . Nhân tiện, ngày. today() trả về một đối tượng ngày tháng, được gán cho biến hôm nay trong chương trình trên. Bây giờ, bạn có thể sử dụng phương thức strftime() để tạo chuỗi biểu thị ngày ở các định dạng khác nhau. today() method to get the current local date. By the way, date. today() returns a date object, which is assigned to the today variable in the above program. Now, you can use the strftime() method to create a string representing date in different formats.

Python có đối tượng ngày không?

Ngày và giờ là một đối tượng trong Python , vì vậy khi bạn thao tác với chúng, bạn thực sự đang thao tác với các đối tượng chứ không phải chuỗi hoặc dấu thời gian. ngày tháng – Một ngày ngây thơ được lý tưởng hóa, giả sử lịch Gregorian hiện tại luôn như vậy và sẽ luôn như vậy, có hiệu lực. Thuộc tính của nó là năm, tháng và ngày.