|
6 | 6 |
|
7 | 7 | import pandas.util._test_decorators as td
|
8 | 8 |
|
9 |
| -from pandas import DataFrame, Index, Series |
| 9 | +from pandas import DataFrame, Index, Series, to_datetime |
10 | 10 | import pandas._testing as tm
|
11 | 11 | from pandas.tests.plotting.common import TestPlotBase, _check_plot_works
|
12 | 12 |
|
@@ -163,17 +163,34 @@ def test_hist_df_legacy(self):
|
163 | 163 | _check_plot_works(self.hist_df.hist)
|
164 | 164 |
|
165 | 165 | # make sure layout is handled
|
166 |
| - df = DataFrame(randn(100, 3)) |
| 166 | + df = DataFrame(randn(100, 2)) |
| 167 | + df[2] = to_datetime( |
| 168 | + np.random.randint( |
| 169 | + self.start_date_to_int64, |
| 170 | + self.end_date_to_int64, |
| 171 | + size=100, |
| 172 | + dtype=np.int64, |
| 173 | + ) |
| 174 | + ) |
167 | 175 | with tm.assert_produces_warning(UserWarning):
|
168 | 176 | axes = _check_plot_works(df.hist, grid=False)
|
169 | 177 | self._check_axes_shape(axes, axes_num=3, layout=(2, 2))
|
170 | 178 | assert not axes[1, 1].get_visible()
|
171 | 179 |
|
| 180 | + _check_plot_works(df[[2]].hist) |
172 | 181 | df = DataFrame(randn(100, 1))
|
173 | 182 | _check_plot_works(df.hist)
|
174 | 183 |
|
175 | 184 | # make sure layout is handled
|
176 |
| - df = DataFrame(randn(100, 6)) |
| 185 | + df = DataFrame(randn(100, 5)) |
| 186 | + df[5] = to_datetime( |
| 187 | + np.random.randint( |
| 188 | + self.start_date_to_int64, |
| 189 | + self.end_date_to_int64, |
| 190 | + size=100, |
| 191 | + dtype=np.int64, |
| 192 | + ) |
| 193 | + ) |
177 | 194 | with tm.assert_produces_warning(UserWarning):
|
178 | 195 | axes = _check_plot_works(df.hist, layout=(4, 2))
|
179 | 196 | self._check_axes_shape(axes, axes_num=6, layout=(4, 2))
|
@@ -225,18 +242,42 @@ def test_hist_df_legacy(self):
|
225 | 242 | ser.hist(foo="bar")
|
226 | 243 |
|
227 | 244 | @pytest.mark.slow
|
228 |
| - def test_hist_non_numerical_raises(self): |
229 |
| - # gh-10444 |
230 |
| - df = DataFrame(np.random.rand(10, 2)) |
| 245 | + def test_hist_non_numerical_or_datetime_raises(self): |
| 246 | + # gh-10444, GH32590 |
| 247 | + df = DataFrame( |
| 248 | + { |
| 249 | + "a": np.random.rand(10), |
| 250 | + "b": np.random.randint(0, 10, 10), |
| 251 | + "c": to_datetime( |
| 252 | + np.random.randint( |
| 253 | + 1582800000000000000, 1583500000000000000, 10, dtype=np.int64 |
| 254 | + ) |
| 255 | + ), |
| 256 | + "d": to_datetime( |
| 257 | + np.random.randint( |
| 258 | + 1582800000000000000, 1583500000000000000, 10, dtype=np.int64 |
| 259 | + ), |
| 260 | + utc=True, |
| 261 | + ), |
| 262 | + } |
| 263 | + ) |
231 | 264 | df_o = df.astype(object)
|
232 | 265 |
|
233 |
| - msg = "hist method requires numerical columns, nothing to plot." |
| 266 | + msg = "hist method requires numerical or datetime columns, nothing to plot." |
234 | 267 | with pytest.raises(ValueError, match=msg):
|
235 | 268 | df_o.hist()
|
236 | 269 |
|
237 | 270 | @pytest.mark.slow
|
238 | 271 | def test_hist_layout(self):
|
239 |
| - df = DataFrame(randn(100, 3)) |
| 272 | + df = DataFrame(randn(100, 2)) |
| 273 | + df[2] = to_datetime( |
| 274 | + np.random.randint( |
| 275 | + self.start_date_to_int64, |
| 276 | + self.end_date_to_int64, |
| 277 | + size=100, |
| 278 | + dtype=np.int64, |
| 279 | + ) |
| 280 | + ) |
240 | 281 |
|
241 | 282 | layout_to_expected_size = (
|
242 | 283 | {"layout": None, "expected_size": (2, 2)}, # default is 2x2
|
@@ -268,7 +309,15 @@ def test_hist_layout(self):
|
268 | 309 | @pytest.mark.slow
|
269 | 310 | # GH 9351
|
270 | 311 | def test_tight_layout(self):
|
271 |
| - df = DataFrame(randn(100, 3)) |
| 312 | + df = DataFrame(np.random.randn(100, 2)) |
| 313 | + df[2] = to_datetime( |
| 314 | + np.random.randint( |
| 315 | + self.start_date_to_int64, |
| 316 | + self.end_date_to_int64, |
| 317 | + size=100, |
| 318 | + dtype=np.int64, |
| 319 | + ) |
| 320 | + ) |
272 | 321 | _check_plot_works(df.hist)
|
273 | 322 | self.plt.tight_layout()
|
274 | 323 |
|
@@ -355,7 +404,15 @@ def test_grouped_hist_legacy(self):
|
355 | 404 |
|
356 | 405 | from pandas.plotting._matplotlib.hist import _grouped_hist
|
357 | 406 |
|
358 |
| - df = DataFrame(randn(500, 2), columns=["A", "B"]) |
| 407 | + df = DataFrame(randn(500, 1), columns=["A"]) |
| 408 | + df["B"] = to_datetime( |
| 409 | + np.random.randint( |
| 410 | + self.start_date_to_int64, |
| 411 | + self.end_date_to_int64, |
| 412 | + size=500, |
| 413 | + dtype=np.int64, |
| 414 | + ) |
| 415 | + ) |
359 | 416 | df["C"] = np.random.randint(0, 4, 500)
|
360 | 417 | df["D"] = ["X"] * 500
|
361 | 418 |
|
|
0 commit comments