Repository: modin-project/modin

Issue:
HDFStore import creates invalid DataFrame
### System information
- **OS Platform and Distribution (e.g., Linux Ubuntu 16.04)**: Arch Linux
- **Modin version** (`modin.__version__`): 0.10.2
- **Python version**: 3.9.6

Code to reproduce:
```python
#!/usr/bin/env python
import modin.pandas as pd
import numpy as np

if __name__ == "__main__":
    # Create a test dataframe
    dfin = pd.DataFrame(np.random.rand(8, 8))
    # and write it to a HDF5 file...
    dfin.to_hdf("hdf.h5", "/key")

    # Open up a HDFStore and read it back in
    with pd.HDFStore("hdf.h5") as h:
        df = pd.read_hdf(h, "/key")
    # Trying to print it fails entirely
    print(df)
```

### Describe the problem
When reading in a HDF5 via a `pandas.HDFStore`, the resulting dataframe is unusable - it appears to have been doubly-wrapped somehow, as the error shows a modin DataFrame being returned by the `.to_pandas()` method.

### Source code / logs
Output of above test script (run in a new venv with `modin[ray]` and `tables` installed):
```
UserWarning: Ray execution environment not yet initialized. Initializing...
To remove this warning, run the following python code before doing dataframe operations:

    import ray
    ray.init()

UserWarning: The size of /dev/shm is too small (8178835456 bytes). The required size at least half of RAM (8349171712 bytes). Please, delete files in /dev/shm or increase size of /dev/shm with --shm-size in Docker. Also, you can set the required memory size for each Ray worker in bytes to MODIN_MEMORY environment variable.
UserWarning: Distributing <class 'numpy.ndarray'> object. This may take some time.
UserWarning: `DataFrame.to_hdf` defaulting to pandas implementation.
To request implementation, send an email to feature_requests@modin.org.
UserWarning: Distributing <class 'pandas.core.frame.DataFrame'> object. This may take some time.
UserWarning: `open` defaulting to pandas implementation.
UserWarning: `read_hdf` defaulting to pandas implementation.
UserWarning: `select` defaulting to pandas implementation.
UserWarning: `get_node` defaulting to pandas implementation.
UserWarning: `close` defaulting to pandas implementation.
Traceback (most recent call last):
  File "/home/jake/./test.py", line 13, in <module>
    print(df)
  File "/home/jake/modin-env/lib/python3.9/site-packages/modin/pandas/base.py", line 3014, in __str__
    return repr(self)
  File "/home/jake/modin-env/lib/python3.9/site-packages/modin/pandas/dataframe.py", line 209, in __repr__
    result = repr(self._build_repr_df(num_rows, num_cols))
  File "/home/jake/modin-env/lib/python3.9/site-packages/modin/pandas/base.py", line 195, in _build_repr_df
    return self.iloc[indexer]._query_compiler.to_pandas()
  File "/home/jake/modin-env/lib/python3.9/site-packages/modin/backends/pandas/query_compiler.py", line 255, in to_pandas
    return self._modin_frame.to_pandas()
  File "/home/jake/modin-env/lib/python3.9/site-packages/modin/engines/base/frame/data.py", line 2227, in to_pandas
    df = self._partition_mgr_cls.to_pandas(self._partitions)
  File "/home/jake/modin-env/lib/python3.9/site-packages/modin/engines/base/frame/partition_manager.py", line 678, in to_pandas
    retrieved_objects = [[obj.to_pandas() for obj in part] for part in partitions]
  File "/home/jake/modin-env/lib/python3.9/site-packages/modin/engines/base/frame/partition_manager.py", line 678, in <listcomp>
    retrieved_objects = [[obj.to_pandas() for obj in part] for part in partitions]
  File "/home/jake/modin-env/lib/python3.9/site-packages/modin/engines/base/frame/partition_manager.py", line 678, in <listcomp>
    retrieved_objects = [[obj.to_pandas() for obj in part] for part in partitions]
  File "/home/jake/modin-env/lib/python3.9/site-packages/modin/engines/ray/pandas_on_ray/frame/partition.py", line 194, in to_pandas
    assert type(dataframe) is pandas.DataFrame or type(dataframe) is pandas.Series
AssertionError
```


Discussion:

Resolving patch:
diff --git a/modin/engines/base/io/io.py b/modin/engines/base/io/io.py
--- a/modin/engines/base/io/io.py
+++ b/modin/engines/base/io/io.py
@@ -518,22 +518,29 @@ def read_hdf(
         chunksize=None,
         **kwargs,
     ):  # noqa: PR01
+        from modin.pandas.io import HDFStore
+
         ErrorMessage.default_to_pandas("`read_hdf`")
-        return cls.from_pandas(
-            pandas.read_hdf(
-                path_or_buf,
-                key=key,
-                mode=mode,
-                columns=columns,
-                errors=errors,
-                where=where,
-                start=start,
-                stop=stop,
-                iterator=iterator,
-                chunksize=chunksize,
-                **kwargs,
-            )
+        modin_store = isinstance(path_or_buf, HDFStore)
+        if modin_store:
+            path_or_buf._return_modin_dataframe = False
+        df = pandas.read_hdf(
+            path_or_buf,
+            key=key,
+            mode=mode,
+            columns=columns,
+            errors=errors,
+            where=where,
+            start=start,
+            stop=stop,
+            iterator=iterator,
+            chunksize=chunksize,
+            **kwargs,
         )
+        if modin_store:
+            path_or_buf._return_modin_dataframe = True
+
+        return cls.from_pandas(df)
 
     @classmethod
     @_inherit_docstrings(pandas.read_feather, apilink="pandas.read_feather")
diff --git a/modin/pandas/io.py b/modin/pandas/io.py
--- a/modin/pandas/io.py
+++ b/modin/pandas/io.py
@@ -633,6 +633,8 @@ def read_xml(
 
 @_inherit_docstrings(pandas.HDFStore)
 class HDFStore(pandas.HDFStore):
+    _return_modin_dataframe = True
+
     def __getattribute__(self, item):
         default_behaviors = ["__init__", "__class__"]
         method = super(HDFStore, self).__getattribute__(item)
@@ -671,7 +673,9 @@ def return_handler(*args, **kwargs):
                         for k, v in kwargs.items()
                     }
                     obj = super(HDFStore, self).__getattribute__(item)(*args, **kwargs)
-                    if isinstance(obj, pandas.DataFrame):
+                    if self._return_modin_dataframe and isinstance(
+                        obj, pandas.DataFrame
+                    ):
                         return DataFrame(obj)
                     return obj
 


Test patch:
diff --git a/modin/pandas/test/test_io.py b/modin/pandas/test/test_io.py
--- a/modin/pandas/test/test_io.py
+++ b/modin/pandas/test/test_io.py
@@ -1712,6 +1712,24 @@ def test_HDFStore(self):
                 os.unlink(hdf_file)
             teardown_test_files([unique_filename_modin, unique_filename_pandas])
 
+    @pytest.mark.xfail(
+        condition="config.getoption('--simulate-cloud').lower() != 'off'",
+        reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264",
+    )
+    def test_HDFStore_in_read_hdf(self):
+        filename = get_unique_filename(extension="hdf")
+        dfin = pd.DataFrame(np.random.rand(8, 8))
+        try:
+            dfin.to_hdf(filename, "/key")
+
+            with pd.HDFStore(filename) as h:
+                modin_df = pd.read_hdf(h, "/key")
+            with pandas.HDFStore(filename) as h:
+                pandas_df = pandas.read_hdf(h, "/key")
+            df_equals(modin_df, pandas_df)
+        finally:
+            teardown_test_files([filename])
+
 
 class TestSql:
     @pytest.mark.xfail(