Skip to content

Commit 3eb63c5

Browse files
committed
TST15519 Moving Unit tests to appropriate file
Moving setUp from tests_dates_generator.py to function scope variable.
1 parent 9b20caa commit 3eb63c5

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

pandas/tests/io/parser/parse_dates.py

+146
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
from pandas.compat import parse_date, StringIO, lrange
2525
from pandas.tseries.index import date_range
2626

27+
# from pandas.io.parsers import read_table
28+
# from pandas.compat.numpy import np_array_datetime64_compat
2729

2830
class ParseDatesTests(object):
2931

@@ -510,3 +512,147 @@ def test_parse_date_time_multi_level_column_name(self):
510512
expected = DataFrame(expected_data,
511513
columns=['date_time', ('A', 'a'), ('B', 'b')])
512514
tm.assert_frame_equal(result, expected)
515+
516+
def test_parse_date_time(self):
517+
# From test_date_coverter
518+
dates = np.array(['2007/1/3', '2008/2/4'], dtype=object)
519+
times = np.array(['05:07:09', '06:08:00'], dtype=object)
520+
expected = np.array([datetime(2007, 1, 3, 5, 7, 9),
521+
datetime(2008, 2, 4, 6, 8, 0)])
522+
523+
result = conv.parse_date_time(dates, times)
524+
self.assertTrue((result == expected).all())
525+
526+
data = """\
527+
date, time, a, b
528+
2001-01-05, 10:00:00, 0.0, 10.
529+
2001-01-05, 00:00:00, 1., 11.
530+
"""
531+
datecols = {'date_time': [0, 1]}
532+
df = read_table(StringIO(data), sep=',', header=0,
533+
parse_dates=datecols, date_parser=conv.parse_date_time)
534+
self.assertIn('date_time', df)
535+
self.assertEqual(df.date_time.loc[0], datetime(2001, 1, 5, 10, 0, 0))
536+
537+
data = ("KORD,19990127, 19:00:00, 18:56:00, 0.8100\n"
538+
"KORD,19990127, 20:00:00, 19:56:00, 0.0100\n"
539+
"KORD,19990127, 21:00:00, 20:56:00, -0.5900\n"
540+
"KORD,19990127, 21:00:00, 21:18:00, -0.9900\n"
541+
"KORD,19990127, 22:00:00, 21:56:00, -0.5900\n"
542+
"KORD,19990127, 23:00:00, 22:56:00, -0.5900")
543+
544+
date_spec = {'nominal': [1, 2], 'actual': [1, 3]}
545+
df = read_csv(StringIO(data), header=None, parse_dates=date_spec,
546+
date_parser=conv.parse_date_time)
547+
548+
def test_parse_date_fields(self):
549+
years = np.array([2007, 2008])
550+
months = np.array([1, 2])
551+
days = np.array([3, 4])
552+
result = conv.parse_date_fields(years, months, days)
553+
expected = np.array([datetime(2007, 1, 3), datetime(2008, 2, 4)])
554+
self.assertTrue((result == expected).all())
555+
556+
data = ("year, month, day, a\n 2001 , 01 , 10 , 10.\n"
557+
"2001 , 02 , 1 , 11.")
558+
datecols = {'ymd': [0, 1, 2]}
559+
df = read_table(StringIO(data), sep=',', header=0,
560+
parse_dates=datecols,
561+
date_parser=conv.parse_date_fields)
562+
self.assertIn('ymd', df)
563+
self.assertEqual(df.ymd.loc[0], datetime(2001, 1, 10))
564+
565+
def test_datetime_six_col(self):
566+
years = np.array([2007, 2008])
567+
months = np.array([1, 2])
568+
days = np.array([3, 4])
569+
hours = np.array([5, 6])
570+
minutes = np.array([7, 8])
571+
seconds = np.array([9, 0])
572+
expected = np.array([datetime(2007, 1, 3, 5, 7, 9),
573+
datetime(2008, 2, 4, 6, 8, 0)])
574+
575+
result = conv.parse_all_fields(years, months, days,
576+
hours, minutes, seconds)
577+
578+
579+
self.assertTrue((result == expected).all())
580+
581+
data = """\
582+
year, month, day, hour, minute, second, a, b
583+
2001, 01, 05, 10, 00, 0, 0.0, 10.
584+
2001, 01, 5, 10, 0, 00, 1., 11.
585+
"""
586+
datecols = {'ymdHMS': [0, 1, 2, 3, 4, 5]}
587+
df = read_table(StringIO(data), sep=',', header=0,
588+
parse_dates=datecols,
589+
date_parser=conv.parse_all_fields)
590+
self.assertIn('ymdHMS', df)
591+
self.assertEqual(df.ymdHMS.loc[0], datetime(2001, 1, 5, 10, 0, 0))
592+
593+
def test_datetime_fractional_seconds(self):
594+
data = """\
595+
year, month, day, hour, minute, second, a, b
596+
2001, 01, 05, 10, 00, 0.123456, 0.0, 10.
597+
2001, 01, 5, 10, 0, 0.500000, 1., 11.
598+
"""
599+
datecols = {'ymdHMS': [0, 1, 2, 3, 4, 5]}
600+
df = read_table(StringIO(data), sep=',', header=0,
601+
parse_dates=datecols,
602+
date_parser=conv.parse_all_fields)
603+
self.assertIn('ymdHMS', df)
604+
self.assertEqual(df.ymdHMS.loc[0], datetime(2001, 1, 5, 10, 0, 0,
605+
microsecond=123456))
606+
self.assertEqual(df.ymdHMS.loc[1], datetime(2001, 1, 5, 10, 0, 0,
607+
microsecond=500000))
608+
609+
def test_generic(self):
610+
data = "year, month, day, a\n 2001, 01, 10, 10.\n 2001, 02, 1, 11."
611+
datecols = {'ym': [0, 1]}
612+
dateconverter = lambda y, m: date(year=int(y), month=int(m), day=1)
613+
df = read_table(StringIO(data), sep=',', header=0,
614+
parse_dates=datecols,
615+
date_parser=dateconverter)
616+
self.assertIn('ym', df)
617+
self.assertEqual(df.ym.loc[0], date(2001, 1, 1))
618+
619+
def test_dateparser_resolution_if_not_ns(self):
620+
# issue 10245
621+
data = """\
622+
date,time,prn,rxstatus
623+
2013-11-03,19:00:00,126,00E80000
624+
2013-11-03,19:00:00,23,00E80000
625+
2013-11-03,19:00:00,13,00E80000
626+
"""
627+
628+
def date_parser(date, time):
629+
datetime = np_array_datetime64_compat(
630+
date + 'T' + time + 'Z', dtype='datetime64[s]')
631+
return datetime
632+
633+
df = read_csv(StringIO(data), date_parser=date_parser,
634+
parse_dates={'datetime': ['date', 'time']},
635+
index_col=['datetime', 'prn'])
636+
637+
datetimes = np_array_datetime64_compat(['2013-11-03T19:00:00Z'] * 3,
638+
dtype='datetime64[s]')
639+
df_correct = DataFrame(data={'rxstatus': ['00E80000'] * 3},
640+
index=MultiIndex.from_tuples(
641+
[(datetimes[0], 126),
642+
(datetimes[1], 23),
643+
(datetimes[2], 13)],
644+
names=['datetime', 'prn']))
645+
assert_frame_equal(df, df_correct)
646+
647+
def test_parse_date_column_with_empty_string(self):
648+
# GH 6428
649+
data = """case,opdate
650+
7,10/18/2006
651+
7,10/18/2008
652+
621, """
653+
result = read_csv(StringIO(data), parse_dates=['opdate'])
654+
expected_data = [[7, '10/18/2006'],
655+
[7, '10/18/2008'],
656+
[621, ' ']]
657+
expected = DataFrame(expected_data, columns=['case', 'opdate'])
658+
assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)