Repository: spyder-ide/spyder

Issue:
Uncaught error when opening a pandas DataFrame with out of bounds timestamps
## Description

**What steps will reproduce the problem?**

1. Create a Dataframe with list of dictionaries
2. two elements of the dictionary are type of datetime
3. Try open it in spyder via click in variable explorer
4. Throws a lot of exceptions until it crashes

**What is the expected output? What do you see instead?**


**Please provide any additional information below**

  File "C:\Users\pfr\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder\widgets\variableexplorer\dataframeeditor.py", line 260, in get_value
    value = self.df.iat[row, column]
  File "C:\Users\pfr\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexing.py", line 1786, in __getitem__
    return self.obj.get_value(*key, takeable=self._takeable)
  File "C:\Users\pfr\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py", line 1817, in get_value
    return _maybe_box_datetimelike(series._values[index])
  File "C:\Users\pfr\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\common.py", line 172, in _maybe_box_datetimelike
    value = tslib.Timestamp(value)
  File "pandas/_libs/tslib.pyx", line 402, in pandas._libs.tslib.Timestamp.__new__ (pandas\_libs\tslib.c:10051)
  File "pandas/_libs/tslib.pyx", line 1517, in pandas._libs.tslib.convert_to_tsobject (pandas\_libs\tslib.c:28598)
  File "pandas/_libs/tslib.pyx", line 1774, in pandas._libs.tslib._check_dts_bounds (pandas\_libs\tslib.c:32752)
pandas._libs.tslib.OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 9999-01-01 00:00:00

Traceback (most recent call last):
  File "C:\Users\pfr\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder\widge


Discussion:
It turns out the issue is related to the representable dates in pandas. My dataset time stamps with 9999-1-1 00:00:00
There is no issue when creating the dataframe, it just fails when you try to open it in the variable viewer.
Ok, thanks for reporting. We just need to catch that error and skip to open our dataframe viewer if it happens.
@pfreiberger how did you create a `DataFrame `with a `datetime `column with timestamps like "9999-1-1 00:00:00" without getting a `OutOfBoundsDatetime` in the first place from pandas? I am not able to reproduce this problem as I am not able to create such a `DataFrame`. Did you use the "import data" button from the variable explorer?
Here is a minimal reproducible example:

```
import pandas as pd
from datetime import datetime
df = pd.DataFrame([{"datetime": datetime.strptime("9999-1-1T00:00", "%Y-%m-%dT%H:%M")}])
```
This will work fine in the iPython console and will show you the dataframe without an issues.
Just when trying to open it in the variable viewer it keeps on crashing.

I hope this helps :)
Oh I see you had to use the `datetime `library. I was using the `to_datetime()` method of a dataframe. Thanks @pfreiberger for the example. It is a feature of pandas to not be able to represent dates way in the future or in the past ([doc](https://pandas.pydata.org/pandas-docs/stable/timeseries.html#timestamp-limitations)).
So I guess it is just a matter of catching the error and representing the value as a string instead (👍) or not displaying the dataframe explorer at all with an error message. 

Resolving patch:
diff --git a/spyder/widgets/variableexplorer/dataframeeditor.py b/spyder/widgets/variableexplorer/dataframeeditor.py
--- a/spyder/widgets/variableexplorer/dataframeeditor.py
+++ b/spyder/widgets/variableexplorer/dataframeeditor.py
@@ -24,6 +24,10 @@
                             QHeaderView)
 
 from pandas import DataFrame, DatetimeIndex, Series
+try:
+    from pandas._libs.tslib import OutOfBoundsDatetime
+except ImportError:  # For pandas version < 0.20
+    from pandas.tslib import OutOfBoundsDatetime
 import numpy as np
 
 # Local imports
@@ -258,6 +262,8 @@ def get_value(self, row, column):
         # handling, so fallback uses iloc
         try:
             value = self.df.iat[row, column]
+        except OutOfBoundsDatetime:
+            value = self.df.iloc[:, column].astype(str).iat[row]
         except:
             value = self.df.iloc[row, column]
         return value


Test patch:
diff --git a/spyder/widgets/variableexplorer/tests/test_dataframeeditor.py b/spyder/widgets/variableexplorer/tests/test_dataframeeditor.py
--- a/spyder/widgets/variableexplorer/tests/test_dataframeeditor.py
+++ b/spyder/widgets/variableexplorer/tests/test_dataframeeditor.py
@@ -11,6 +11,7 @@
 
 # Standard library imports
 from sys import platform
+from datetime import datetime
 try:
     from unittest.mock import Mock, ANY
 except ImportError:
@@ -231,6 +232,14 @@ def test_dataframeeditor_with_datetimeindex():
     assert data(dfm, 1, 1) == '2015-01-02 00:00:00'
     assert data(dfm, 2, 1) == '2015-01-03 00:00:00'
 
+def test_dataframeeditor_with_OutOfBoundsDatetime():  # Test for #6177
+    df = DataFrame([{'DATETIME': datetime.strptime("9999-1-1T00:00",
+                                                   "%Y-%m-%dT%H:%M")}])
+    model = DataFrameModel(df)
+    try:
+        model.get_value(0, 0)
+    except Exception:
+        assert False
 
 @pytest.mark.skipif(not os.name == 'nt',
                     reason="It segfaults too much on Linux")