正在处理的时间戳数据中存在无效或意外值,例如负年份或空值。如何解决这个问题,我们需要确保时间戳值的格式正确并且在转换为Python日期时间对象的有效范围内。
import psycopg2
import pandas as pd
# Create a cursor object
cur = conn.cursor()
# Execute your SQL query to access a table
try:
# Retrieve the minimum date
cur.execute("""
SELECT MIN(dt_date)
FROM prod_external.tran_y_register
WHERE entry_type = 1;
""")
min_date = cur.fetchone()[0] # Get the minimum date from the result
# Fetch all data from the table starting from the minimum date
cur.execute("""
SELECT *
FROM prod_external.tran_y_register
WHERE entry_type = 1
AND dt_date >= %s
ORDER BY dt_date;
""", (min_date,))
# Fetch rows as tuples
rows = cur.fetchall()
# Extract column names
column_names = [desc[0] for desc in cur.description]
# Convert rows to DataFrame
df = pd.DataFrame(rows, columns=column_names)
# Convert 'dt_date' column to Pandas Timestamp objects manually
df['dt_date'] = pd.to_datetime(df['dt_date'], errors='coerce') # Handle any invalid timestamps
# Drop rows with NaT (invalid timestamps)
df.dropna(subset=['dt_date'], inplace=True)
# Print DataFrame
print(df)
except psycopg2.Error as e:
print("Error: Could not fetch data from the table")
print(e)
# Close cursor and connection
cur.close()
conn.close()
我收到错误:ValueError:year -1 is out of range
您遇到的错误表明时间戳数据中存在负年份,这些年份超出了转换为 Python 日期时间对象的范围。为了解决此问题,您可以在将数据转换为日期时间对象之前对数据进行预处理以处理无效或意外的值。
df = df[df['dt_date'].dt.year > 0]
print(df)