Esempio n. 1
0
    def test_to_datetime(self):
        pdf = pd.DataFrame({
            "year": [2015, 2016],
            "month": [2, 3],
            "day": [4, 5]
        })
        psdf = ps.from_pandas(pdf)
        dict_from_pdf = pdf.to_dict()

        self.assert_eq(pd.to_datetime(pdf), ps.to_datetime(psdf))
        self.assert_eq(pd.to_datetime(dict_from_pdf),
                       ps.to_datetime(dict_from_pdf))

        self.assert_eq(pd.to_datetime(1490195805, unit="s"),
                       ps.to_datetime(1490195805, unit="s"))
        self.assert_eq(
            pd.to_datetime(1490195805433502912, unit="ns"),
            ps.to_datetime(1490195805433502912, unit="ns"),
        )

        self.assert_eq(
            pd.to_datetime([1, 2, 3],
                           unit="D",
                           origin=pd.Timestamp("1960-01-01")),
            ps.to_datetime([1, 2, 3],
                           unit="D",
                           origin=pd.Timestamp("1960-01-01")),
        )
Esempio n. 2
0
    def test_to_datetime(self):
        pdf = pd.DataFrame({
            "year": [2015, 2016],
            "month": [2, 3],
            "day": [4, 5]
        })
        psdf = ps.from_pandas(pdf)
        dict_from_pdf = pdf.to_dict()

        self.assert_eq(pd.to_datetime(pdf), ps.to_datetime(psdf))
        self.assert_eq(pd.to_datetime(dict_from_pdf),
                       ps.to_datetime(dict_from_pdf))

        self.assert_eq(pd.to_datetime(1490195805, unit="s"),
                       ps.to_datetime(1490195805, unit="s"))
        self.assert_eq(
            pd.to_datetime(1490195805433502912, unit="ns"),
            ps.to_datetime(1490195805433502912, unit="ns"),
        )

        self.assert_eq(
            pd.to_datetime([1, 2, 3],
                           unit="D",
                           origin=pd.Timestamp("1960-01-01")),
            ps.to_datetime([1, 2, 3],
                           unit="D",
                           origin=pd.Timestamp("1960-01-01")),
        )

        pdf = pd.DataFrame({
            "years": [2015, 2016],
            "month": [2, 3],
            "day": [4, 5]
        })
        psdf = ps.from_pandas(pdf)
        dict_from_pdf = pdf.to_dict()

        self.assert_eq(pd.to_datetime(pdf), ps.to_datetime(psdf))
        self.assert_eq(pd.to_datetime(dict_from_pdf),
                       ps.to_datetime(dict_from_pdf))

        pdf = pd.DataFrame({
            "years": [2015, 2016],
            "months": [2, 3],
            "day": [4, 5]
        })
        psdf = ps.from_pandas(pdf)
        dict_from_pdf = pdf.to_dict()

        self.assert_eq(pd.to_datetime(pdf), ps.to_datetime(psdf))
        self.assert_eq(pd.to_datetime(dict_from_pdf),
                       ps.to_datetime(dict_from_pdf))

        pdf = pd.DataFrame({
            "years": [2015, 2016],
            "months": [2, 3],
            "days": [4, 5]
        })
        psdf = ps.from_pandas(pdf)
        dict_from_pdf = pdf.to_dict()

        self.assert_eq(pd.to_datetime(pdf), ps.to_datetime(psdf))
        self.assert_eq(pd.to_datetime(dict_from_pdf),
                       ps.to_datetime(dict_from_pdf))

        # SPARK-36946: Support time for ps.to_datetime
        pdf = pd.DataFrame({
            "year": [2015, 2016],
            "month": [2, 3],
            "day": [4, 5],
            "hour": [2, 3],
            "minute": [10, 30],
            "second": [21, 25],
        })
        psdf = ps.from_pandas(pdf)
        dict_from_pdf = pdf.to_dict()

        self.assert_eq(pd.to_datetime(pdf), ps.to_datetime(psdf))
        self.assert_eq(pd.to_datetime(dict_from_pdf),
                       ps.to_datetime(dict_from_pdf))

        pdf = pd.DataFrame({
            "year": [2015, 2016],
            "month": [2, 3],
            "day": [4, 5],
            "hour": [2, 3],
            "minute": [10, 30],
        })
        psdf = ps.from_pandas(pdf)
        dict_from_pdf = pdf.to_dict()

        self.assert_eq(pd.to_datetime(pdf), ps.to_datetime(psdf))
        self.assert_eq(pd.to_datetime(dict_from_pdf),
                       ps.to_datetime(dict_from_pdf))

        pdf = pd.DataFrame({
            "year": [2015, 2016],
            "month": [2, 3],
            "day": [4, 5],
            "hour": [2, 3]
        })
        psdf = ps.from_pandas(pdf)
        dict_from_pdf = pdf.to_dict()

        self.assert_eq(pd.to_datetime(pdf), ps.to_datetime(psdf))
        self.assert_eq(pd.to_datetime(dict_from_pdf),
                       ps.to_datetime(dict_from_pdf))

        pdf = pd.DataFrame({
            "year": [2015, 2016],
            "month": [2, 3],
            "day": [4, 5],
            "hour": [2, 3],
            "minute": [10, 30],
            "second": [21, 25],
            "ms": [50, 69],
        })
        psdf = ps.from_pandas(pdf)
        dict_from_pdf = pdf.to_dict()

        self.assert_eq(pd.to_datetime(pdf), ps.to_datetime(psdf))
        self.assert_eq(pd.to_datetime(dict_from_pdf),
                       ps.to_datetime(dict_from_pdf))

        pdf = pd.DataFrame({
            "year": [2015, 2016],
            "month": [2, 3],
            "day": [4, 5],
            "hour": [2, 3],
            "minute": [10, 30],
            "second": [21, 25],
            "ms": [50, 69],
            "millisecond": [123, 678],
        })
        psdf = ps.from_pandas(pdf)
        dict_from_pdf = pdf.to_dict()

        self.assert_eq(pd.to_datetime(pdf), ps.to_datetime(psdf))
        self.assert_eq(pd.to_datetime(dict_from_pdf),
                       ps.to_datetime(dict_from_pdf))

        pdf = pd.DataFrame({
            "Year": [2015, 2016],
            "Month": [2, 3],
            "Day": [4, 5],
            "Hour": [2, 3],
            "Minute": [10, 30],
            "Second": [21, 25],
            "ms": [50, 69],
            "millisecond": [123, 678],
        })
        psdf = ps.from_pandas(pdf)
        dict_from_pdf = pdf.to_dict()

        self.assert_eq(pd.to_datetime(pdf), ps.to_datetime(psdf))
        self.assert_eq(pd.to_datetime(dict_from_pdf),
                       ps.to_datetime(dict_from_pdf))