-1

Using pd.date_range gives time stamps with dates in yyyy-mm-dd format and additional information in hours, mins etc. I want to go from here to a simple dataframe with a list of dates only in mm/dd/yyyy format only. I have tried converting things to strings, array of strings etc, modifying those , but it is all horribly convoluted.

Does anyone know of a straightforward method? There must be one; this must be a common problem.

Edit/Addendum: Jason Dixon's reply hit the nail on the head. I had already tried all the other things some posters suggested. Thank you all.

user2133151
  • 247
  • 1
  • 2
  • 10

2 Answers2

1

There are a few solutions possible, but I believe they will all involve Python string formatting.

Pandas will allow you to write a function - e.g., to convert a date to a string - and then apply that function to each row of a column. This feature uses the apply() function in Pandas.

Function to convert date to a string:

def convert_date_standard_string(datelike_object):

    """
    Return string version of date in format mm/dd/yyyy

    Parameters
    -----------

    datelike_object
        A value of type date, datetime, or Timestamp.

        (e.g., Python datetime.datetime, datetime.date,
        Pandas Timestamp)

    """

    return "{:%m/%d/%Y}".format(datelike_object)

Most of this function is documentation. The key part is return "{:%m/%d/%Y}".format(datelike_object), which uses Python string formatting to put the date into the desired format.

Here is an example DataFrame with a column created from the date_range function.

>>> dates_list = pd.date_range(start="01/01/2019", end="01/31/2019")

>>> df = pd.DataFrame(dates_list, columns=["Dates"])

>>> df

        Dates
0  2019-01-01
1  2019-01-02
2  2019-01-03
3  2019-01-04
4  2019-01-05
5  2019-01-06
6  2019-01-07
7  2019-01-08
8  2019-01-09
9  2019-01-10
10 2019-01-11
...
27 2019-01-28
28 2019-01-29
29 2019-01-30
30 2019-01-31

Note: Although the dates are displayed with only the year, month, and day, each value actually holds the time information also, as the OP pointed out. In addition, the format is not what the OP needs.

>>> df["Dates"].loc[0]
Timestamp('2019-01-01 00:00:00')

Here is the syntax to apply the custom function created above to this column:

df["Dates"].apply(convert_date_standard_string)

Typically I would create a new column to store the converted values, like this:

>>> df["Dates - mm/dd/yyyy"] = df["Dates"].apply(convert_date_standard_string)

>>> df

        Dates Dates - mm/dd/yyyy
0  2019-01-01         01/01/2019
1  2019-01-02         01/02/2019
2  2019-01-03         01/03/2019
3  2019-01-04         01/04/2019
4  2019-01-05         01/05/2019
5  2019-01-06         01/06/2019
6  2019-01-07         01/07/2019
7  2019-01-08         01/08/2019
8  2019-01-09         01/09/2019
9  2019-01-10         01/10/2019
10 2019-01-11         01/11/2019
...
27 2019-01-28         01/28/2019
28 2019-01-29         01/29/2019
29 2019-01-30         01/30/2019
30 2019-01-31         01/31/2019

To demonstrate that the new column is a string:

>>> df["Dates - mm/dd/yyyy"].loc[0]
'01/01/2019'
>>> type(df["Dates - mm/dd/yyyy"].loc[0])
<class 'str'>
Jason
  • 26
  • 2
  • Jason, Your "convert_date_standard_string" is exactly what I was looking for. Thanks a bunch. I had tried all other things suggested here. Thanks to the others who replied as well. – user2133151 Dec 31 '18 at 15:25
0

You could use dt.strftime() (with '%m/%d/%Y' pattern in your case) on your column containing datetime64 entries. If they aren't of this format yet, try converting the column first by running pd.to_datetime() on it.

gosuto
  • 5,422
  • 6
  • 36
  • 57