I am trying to read some sql data using pandas library and one of the column “customer_date” has values like “0001-11-29 13:00:00 BC”. My query fails with error
ValueError: year 0 is out of range
Please suggest a way to parse such date/timestamps.
Here is my code.
JavaScript
x
13
13
1
import pandas as pd
2
from datetime import datetime, timedelta
3
4
session = ah.get_boto_session()
5
db = get_connection() **### Custom method**
6
7
pd.set_option('display.max_rows', None)
8
query = "SELECT customer_date FROM customer"
9
df = pd.read_sql(query, db, parse_dates=["customer_date"])
10
print(df.head())
11
12
db.dispose()
13
Error:
JavaScript
1
10
10
1
---------------------------------------------------------------------------
2
ValueError Traceback (most recent call last)
3
<ipython-input-2-d4e334c3f39c> in <module>()
4
22 pd.set_option('display.max_rows', None)
5
23 query = "SELECT customer_date FROM customer"
6
---> 24 df = pd.read_sql(query, db, parse_dates=["customer_date"])
7
25 print(df.head())
8
9
ValueError: year 0 is out of range
10
Advertisement
Answer
This is what worked:
JavaScript
1
13
13
1
import pandas as pd
2
from datetime import datetime, timedelta
3
4
session = ah.get_boto_session()
5
db = get_connection() **### Custom method**
6
7
pd.set_option('display.max_rows', None)
8
query = "SELECT case when customer_date >= '1970-01-01' then customer_date else '1970-01-01' end AS customer_date FROM customer"
9
df = pd.read_sql(query, db)
10
print(df.head())
11
12
db.dispose()
13