@@ -456,6 +456,28 @@ def test_to_string_with_formatters(self):
456
456
'2 0x3 [ 3.0] -False-' ))
457
457
self .assertEqual (result , result2 )
458
458
459
+ def test_to_string_with_datetime64_monthformatter (self ):
460
+ months = [datetime (2016 , 1 , 1 ), datetime (2016 , 2 , 2 )]
461
+ x = DataFrame ({'months' : months })
462
+
463
+ def format_func (x ):
464
+ return x .strftime ('%Y-%m' )
465
+ result = x .to_string (formatters = {'months' : format_func })
466
+ expected = 'months\n 0 2016-01\n 1 2016-02'
467
+ self .assertEqual (result .strip (), expected )
468
+
469
+ def test_to_string_with_datetime64_hourformatter (self ):
470
+
471
+ x = DataFrame ({'hod' : pd .to_datetime (['10:10:10.100' , '12:12:12.120' ],
472
+ format = '%H:%M:%S.%f' )})
473
+
474
+ def format_func (x ):
475
+ return x .strftime ('%H:%M' )
476
+
477
+ result = x .to_string (formatters = {'hod' : format_func })
478
+ expected = 'hod\n 0 10:10\n 1 12:12'
479
+ self .assertEqual (result .strip (), expected )
480
+
459
481
def test_to_string_with_formatters_unicode (self ):
460
482
df = DataFrame ({u ('c/\u03c3 ' ): [1 , 2 , 3 ]})
461
483
result = df .to_string (formatters = {u ('c/\u03c3 ' ): lambda x : '%s' % x })
@@ -1233,6 +1255,63 @@ def test_to_html_index_formatter(self):
1233
1255
1234
1256
self .assertEqual (result , expected )
1235
1257
1258
+ def test_to_html_datetime64_monthformatter (self ):
1259
+ months = [datetime (2016 , 1 , 1 ), datetime (2016 , 2 , 2 )]
1260
+ x = DataFrame ({'months' : months })
1261
+
1262
+ def format_func (x ):
1263
+ return x .strftime ('%Y-%m' )
1264
+ result = x .to_html (formatters = {'months' : format_func })
1265
+ expected = """\
1266
+ <table border="1" class="dataframe">
1267
+ <thead>
1268
+ <tr style="text-align: right;">
1269
+ <th></th>
1270
+ <th>months</th>
1271
+ </tr>
1272
+ </thead>
1273
+ <tbody>
1274
+ <tr>
1275
+ <th>0</th>
1276
+ <td>2016-01</td>
1277
+ </tr>
1278
+ <tr>
1279
+ <th>1</th>
1280
+ <td>2016-02</td>
1281
+ </tr>
1282
+ </tbody>
1283
+ </table>"""
1284
+ self .assertEqual (result , expected )
1285
+
1286
+ def test_to_html_datetime64_hourformatter (self ):
1287
+
1288
+ x = DataFrame ({'hod' : pd .to_datetime (['10:10:10.100' , '12:12:12.120' ],
1289
+ format = '%H:%M:%S.%f' )})
1290
+
1291
+ def format_func (x ):
1292
+ return x .strftime ('%H:%M' )
1293
+ result = x .to_html (formatters = {'hod' : format_func })
1294
+ expected = """\
1295
+ <table border="1" class="dataframe">
1296
+ <thead>
1297
+ <tr style="text-align: right;">
1298
+ <th></th>
1299
+ <th>hod</th>
1300
+ </tr>
1301
+ </thead>
1302
+ <tbody>
1303
+ <tr>
1304
+ <th>0</th>
1305
+ <td>10:10</td>
1306
+ </tr>
1307
+ <tr>
1308
+ <th>1</th>
1309
+ <td>12:12</td>
1310
+ </tr>
1311
+ </tbody>
1312
+ </table>"""
1313
+ self .assertEqual (result , expected )
1314
+
1236
1315
def test_to_html_regression_GH6098 (self ):
1237
1316
df = DataFrame ({u ('clé1' ): [u ('a' ), u ('a' ), u ('b' ), u ('b' ), u ('a' )],
1238
1317
u ('clé2' ): [u ('1er' ), u ('2ème' ), u ('1er' ), u ('2ème' ),
@@ -2775,6 +2854,33 @@ def test_to_latex_format(self):
2775
2854
2776
2855
self .assertEqual (withindex_result , withindex_expected )
2777
2856
2857
+ def test_to_latex_with_formatters (self ):
2858
+ df = DataFrame ({'int' : [1 , 2 , 3 ],
2859
+ 'float' : [1.0 , 2.0 , 3.0 ],
2860
+ 'object' : [(1 , 2 ), True , False ],
2861
+ 'datetime64' : [datetime (2016 , 1 , 1 ),
2862
+ datetime (2016 , 2 , 5 ),
2863
+ datetime (2016 , 3 , 3 )]})
2864
+
2865
+ formatters = {'int' : lambda x : '0x%x' % x ,
2866
+ 'float' : lambda x : '[% 4.1f]' % x ,
2867
+ 'object' : lambda x : '-%s-' % str (x ),
2868
+ 'datetime64' : lambda x : x .strftime ('%Y-%m' ),
2869
+ '__index__' : lambda x : 'index: %s' % x }
2870
+ result = df .to_latex (formatters = dict (formatters ))
2871
+
2872
+ expected = r"""\begin{tabular}{llrrl}
2873
+ \toprule
2874
+ {} & datetime64 & float & int & object \\
2875
+ \midrule
2876
+ index: 0 & 2016-01 & [ 1.0] & 0x1 & -(1, 2)- \\
2877
+ index: 1 & 2016-02 & [ 2.0] & 0x2 & -True- \\
2878
+ index: 2 & 2016-03 & [ 3.0] & 0x3 & -False- \\
2879
+ \bottomrule
2880
+ \end{tabular}
2881
+ """
2882
+ self .assertEqual (result , expected )
2883
+
2778
2884
def test_to_latex_multiindex (self ):
2779
2885
df = DataFrame ({('x' , 'y' ): ['a' ]})
2780
2886
result = df .to_latex ()
@@ -4161,6 +4267,28 @@ def test_dates_display(self):
4161
4267
self .assertEqual (result [1 ].strip (), "NaT" )
4162
4268
self .assertEqual (result [4 ].strip (), "2013-01-01 09:00:00.000000004" )
4163
4269
4270
+ def test_datetime64formatter_yearmonth (self ):
4271
+ x = Series ([datetime (2016 , 1 , 1 ), datetime (2016 , 2 , 2 )])
4272
+
4273
+ def format_func (x ):
4274
+ return x .strftime ('%Y-%m' )
4275
+
4276
+ formatter = fmt .Datetime64Formatter (x , formatter = format_func )
4277
+ result = formatter .get_result ()
4278
+ self .assertEqual (result , ['2016-01' , '2016-02' ])
4279
+
4280
+ def test_datetime64formatter_hoursecond (self ):
4281
+
4282
+ x = Series (pd .to_datetime (['10:10:10.100' , '12:12:12.120' ],
4283
+ format = '%H:%M:%S.%f' ))
4284
+
4285
+ def format_func (x ):
4286
+ return x .strftime ('%H:%M' )
4287
+
4288
+ formatter = fmt .Datetime64Formatter (x , formatter = format_func )
4289
+ result = formatter .get_result ()
4290
+ self .assertEqual (result , ['10:10' , '12:12' ])
4291
+
4164
4292
4165
4293
class TestNaTFormatting (tm .TestCase ):
4166
4294
0 commit comments