@@ -110,6 +110,19 @@ def test_correct_appending(self, _tfs_file_x_pathlib, _tfs_file_y_pathlib, how_h
110110 assert_dict_equal (result .headers , merge_headers (dframe_x .headers , dframe_y .headers , how = how_headers ))
111111 assert_frame_equal (result , pd .DataFrame (dframe_x ).append (pd .DataFrame (dframe_y )))
112112
113+ @pytest .mark .parametrize ("how_headers" , [None , "left" , "right" ])
114+ def test_appending_accepts_pandas_dataframe (self , _tfs_file_x_pathlib , _tfs_file_y_pathlib , how_headers ):
115+ dframe_x = tfs .read (_tfs_file_x_pathlib )
116+ dframe_y = pd .DataFrame (tfs .read (_tfs_file_y_pathlib )) # for test, loses headers here
117+ result = dframe_x .append (dframe_y , how_headers = how_headers )
118+
119+ assert isinstance (result , TfsDataFrame )
120+ assert isinstance (result .headers , OrderedDict )
121+
122+ # using empty OrderedDict here as it's what dframe_y is getting when converted in the call
123+ assert_dict_equal (result .headers , merge_headers (dframe_x .headers , OrderedDict (), how = how_headers ))
124+ assert_frame_equal (result , pd .DataFrame (dframe_x ).append (dframe_y )) # dframe_y already pandas
125+
113126
114127class TestTfsDataFrameJoining :
115128 @pytest .mark .parametrize ("how_headers" , [None , "left" , "right" ])
@@ -127,6 +140,25 @@ def test_correct_joining(self, _tfs_file_x_pathlib, _tfs_file_y_pathlib, how_hea
127140 result , pd .DataFrame (dframe_x ).join (pd .DataFrame (dframe_y ), lsuffix = lsuffix , rsuffix = rsuffix )
128141 )
129142
143+ @pytest .mark .parametrize ("how_headers" , [None , "left" , "right" ])
144+ @pytest .mark .parametrize ("lsuffix" , ["left" , "_x" ])
145+ @pytest .mark .parametrize ("rsuffix" , ["right" , "_y" ])
146+ def test_joining_accepts_pandas_dataframe (
147+ self , _tfs_file_x_pathlib , _tfs_file_y_pathlib , how_headers , lsuffix , rsuffix
148+ ):
149+ dframe_x = tfs .read (_tfs_file_x_pathlib )
150+ dframe_y = pd .DataFrame (tfs .read (_tfs_file_y_pathlib )) # for test, loses headers here
151+ result = dframe_x .join (dframe_y , how_headers = how_headers , lsuffix = lsuffix , rsuffix = rsuffix )
152+
153+ assert isinstance (result , TfsDataFrame )
154+ assert isinstance (result .headers , OrderedDict )
155+
156+ # using empty OrderedDict here as it's what dframe_y is getting when converted in the call
157+ assert_dict_equal (result .headers , merge_headers (dframe_x .headers , OrderedDict (), how = how_headers ))
158+ assert_frame_equal (
159+ result , pd .DataFrame (dframe_x ).join (pd .DataFrame (dframe_y ), lsuffix = lsuffix , rsuffix = rsuffix )
160+ )
161+
130162
131163class TestTfsDataFrameMerging :
132164 @pytest .mark .parametrize ("how_headers" , [None , "left" , "right" ])
@@ -142,6 +174,23 @@ def test_correct_merging(self, _tfs_file_x_pathlib, _tfs_file_y_pathlib, how_hea
142174 assert_dict_equal (result .headers , merge_headers (dframe_x .headers , dframe_y .headers , how = how_headers ))
143175 assert_frame_equal (result , pd .DataFrame (dframe_x ).merge (pd .DataFrame (dframe_y ), how = how , on = on ))
144176
177+ @pytest .mark .parametrize ("how_headers" , [None , "left" , "right" ])
178+ @pytest .mark .parametrize ("how" , ["left" , "right" , "outer" , "inner" ])
179+ @pytest .mark .parametrize ("on" , ["NAME" , "S" , "NUMBER" , "CO" , "CORMS" , "BPM_RES" ])
180+ def test_merging_accepts_pandas_dataframe (
181+ self , _tfs_file_x_pathlib , _tfs_file_y_pathlib , how_headers , how , on
182+ ):
183+ dframe_x = tfs .read (_tfs_file_x_pathlib )
184+ dframe_y = pd .DataFrame (tfs .read (_tfs_file_y_pathlib )) # for test, loses headers here
185+ result = dframe_x .merge (dframe_y , how_headers = how_headers , how = how , on = on )
186+
187+ assert isinstance (result , TfsDataFrame )
188+ assert isinstance (result .headers , OrderedDict )
189+
190+ # using empty OrderedDict here as it's what dframe_y is getting when converted in the call
191+ assert_dict_equal (result .headers , merge_headers (dframe_x .headers , OrderedDict (), how = how_headers ))
192+ assert_frame_equal (result , pd .DataFrame (dframe_x ).merge (pd .DataFrame (dframe_y ), how = how , on = on ))
193+
145194
146195class TestTfsDataFramesConcatenating :
147196 @pytest .mark .parametrize ("how_headers" , [None , "left" , "right" ])
@@ -160,6 +209,28 @@ def test_correct_concatenating(self, _tfs_file_x_pathlib, _tfs_file_y_pathlib, h
160209 assert_dict_equal (result .headers , reduce (merger , all_headers ))
161210 assert_frame_equal (result , pd .concat (objs , axis = axis , join = join ))
162211
212+ @pytest .mark .parametrize ("how_headers" , [None , "left" , "right" ])
213+ @pytest .mark .parametrize ("axis" , [0 , 1 ])
214+ @pytest .mark .parametrize ("join" , ["inner" , "outer" ])
215+ def test_concatenating_accepts_pandas_dataframes (
216+ self , _tfs_file_x_pathlib , _tfs_file_y_pathlib , how_headers , axis , join
217+ ):
218+ dframe_x = tfs .read (_tfs_file_x_pathlib )
219+ dframe_y = pd .DataFrame (tfs .read (_tfs_file_y_pathlib )) # for test, loses headers here
220+ objs = [dframe_x ] * 4 + [dframe_y ] * 4 # now has a mix of TfsDataFrames and pandas.DataFrames
221+ result = concat (objs , how_headers = how_headers , axis = axis , join = join )
222+
223+ merger = partial (merge_headers , how = how_headers )
224+ # all_headers = (tfsdframe.headers for tfsdframe in objs)
225+ assert isinstance (result , TfsDataFrame )
226+ assert isinstance (result .headers , OrderedDict )
227+
228+ all_headers = [ # empty OrderedDicts here as it's what objects are getting when converted in the call
229+ dframe .headers if isinstance (dframe , TfsDataFrame ) else OrderedDict () for dframe in objs
230+ ]
231+ assert_dict_equal (result .headers , reduce (merger , all_headers ))
232+ assert_frame_equal (result , pd .concat (objs , axis = axis , join = join ))
233+
163234
164235# ------ Fixtures ------ #
165236
0 commit comments